Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change text while running the console [duplicate]

Tags:

c++

Possible Duplicate:
How to delete printed characters from command line in C++

my question is, how do I change text while RUNNING a console window in C++. For example.

If I were to display this.

cout<<"0%";
cout<<"25%";
cout<<50%";
cout<<75%";
cout<<"100%";

It will make 5 different words. What if I want it to display 0% then 25% withought making a new word, I.E replacing the current 0% with a 25%. Is this even possible? Thanks in advance.

like image 864
user1930233 Avatar asked Dec 26 '12 16:12

user1930233


1 Answers

Use cout << number << '\r' << flush.

The '\r' means "carriage return" (go to beginning of line", the flush means "make sure what I've just printed reaches the output now. Normally output is only printed when a end of line is provided.

Edit: If you have a situation where the length of the output varies, e.g. counting down, you will have to pad the output with sufficient spaces to cover any extra output. For example cout << setw(3) << number ... or cout << number << " " ... would work.

Be aware, however, if your line gets longer than the width of the termina/command windo, it may become messy.

like image 118
Mats Petersson Avatar answered Nov 15 '22 02:11

Mats Petersson