所以我做了一個草率的時鐘程式作為作業。它作業正常,但是我希望輸出將每個 cout 顯示為"01:02",而不是顯示"1:2",即使我認為我已經撰寫了所有正確的操縱器。這是我的整個程式。
#include <thread>
#include <chrono>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
using namespace std;
int main()
{
cout << fixed << right;
cout.fill(0);
for (int m = 0; m < 59; m )
{
for (int s = 0; s < 59; s )
{
this_thread::sleep_for(
chrono::seconds(1));
system("cls");
cout << setw(2) << m << ":";
cout << setw(2) << s << endl;
}
}
}
uj5u.com熱心網友回復:
您還需要setfill('0')使用setw().
setw()只需設定格式化輸出操作的最小寬度。欄位寬度的未使用部分默認為空格。setfill()改變了這一點。
uj5u.com熱心網友回復:
您正在使用空字符來填充(不可見)。
cout.fill(0);
您可能的意思是使用 ASCII 字符0,如下所示:
cout.fill('0');
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/429316.html
標籤:C
上一篇:c std::vector<NotAPointer>如何存盤不同大小的物件,當它不包含指標時, it如何知道跳轉的位置
下一篇:g 編譯makefile有點麻煩
