0
我想做一個非常基本的進度指示器,通過打印一個'X'字符到cout每次循環進行時cout %。我正在試圖做到這一點,如下面粘貼的代碼所示,但它似乎並沒有工作 - 當它真的看起來應該。設計一個簡單的進度條到cout從0-> 100%立即在C++結束
它應該在整個循環中顯示穩定的進度,但是在循環結束後我會立即得到所有的X。這並不是因爲循環完成得太快而讓我無法察覺。要驗證這一點,您可以在「TOTAL」中添加一個額外的'F'以大幅增加循環的持續時間,並且您會發現它不僅僅是知覺問題。
任何想法可能導致這種情況?
#include <iostream>
#define TOTAL 0xFFFFFFF
using namespace std;
int main(void) {
//Make a counter for counting loops
double counter = 0;
// Set it to trigger after each 10% of progress
double counterMax = TOTAL/10;
cout << "Starting now..." << endl;
for (double i = 0; i < TOTAL; i++) {
// Do something... anything
i++;
i--;
// Increment the counter
counter++;
// Print an X when the counter exceeds the 10%
// trigger point, and then reset the counter.
if (counter > counterMax) {
cout << 'X';
counter = 0;
}
}
cout << endl << "Done!";
return 0;
}
你應該加上'cout.flush():但是你可以叫
cout.flush()
或使用flush
機械手,就像在任何點觸發標準輸出的沖洗;'。 –或者,'cout <<'X'<< flush;' – AJNeufeld
完美工作。謝謝你們! – Eddie