With a fixed width of your output, use something like the following:
float progress = 0.0; while (progress < 1.0) { int barWidth = 70; std::cout << "["; int pos = barWidth * progress; for (int i = 0; i < barWidth; ++i) { if (i < pos) std::cout << "="; else if (i == pos) std::cout << ">"; else std::cout << " "; } std::cout << "] " << int(progress * 100.0) << " %\r"; std::cout.flush(); progress += 0.16;
http://ideone.com/Yg8NKj
[> ] 0 % [===========> ] 15 % [======================> ] 31 % [=================================> ] 47 % [============================================> ] 63 % [========================================================> ] 80 % [===================================================================> ] 96 %
Please note that this output is displayed on one line below each other, but in the terminal emulator (I think also on the Windows command line) it will be printed on the same line.
At the very end, remember to print a new line before printing more things.
If you want to remove the panel at the end, you must rewrite it with spaces to print something shorter, for example, "Done." .
In addition, the same can be done using printf in C; adaptation of the above code should be straightforward.
leemes Jan 26 '13 at 18:11 2013-01-26 18:11
source share