Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ changing output on console

Tags:

c++

console

What is the easiest way to display changing numbers in the console? I have a normal command line program in C++ which uses cout, but I'd like to display a percentage number representing the progress which counts up to 100 without printing a new line. How is that done? (If it matters: I'm on Windows 7)

like image 941
Felix Dombek Avatar asked Feb 13 '11 05:02

Felix Dombek


2 Answers

When I’ve needed that I have just output a carriage return character, in C++ \r.

Remember to flush the output each time, e.g.

cout << "\r" << x << "% completed.       " << flush;

The spaces at the end to clear previous output on the line in case of Microsoft-like fluctuating progress.

enter image description here

like image 139
Cheers and hth. - Alf Avatar answered Sep 18 '22 17:09

Cheers and hth. - Alf


Use the backspace character.

cout << "10%";
// ...
cout << "\b\b\b20%";
like image 25
dan04 Avatar answered Sep 17 '22 17:09

dan04