6

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.

Community
  • 1
  • 1
user1930233
  • 75
  • 1
  • 1
  • 6

1 Answers1

17

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • but if you print "25%" and then "0%", will it show up as "0%%" or will the original "%" disappear? – eds Dec 26 '12 at 16:18
  • Updated my answer to cover that part. – Mats Petersson Dec 26 '12 at 16:21
  • So basically I need to type: cout<<"10%" << '/r' << flush; cout<<"20%"? – user1930233 Dec 26 '12 at 16:25
  • Yes, but I presume you want to do something more in between? – Mats Petersson Dec 26 '12 at 16:26
  • If I want to convert the cout<<"0%"; cout<<"25%"; cout<<50%"; cout<<75%"; cout<<"100%"; Can you give me the code to display it with the replace line? – user1930233 Dec 26 '12 at 16:29
  • What are you asking for? As you wrote, that's what you need to do - I'm just assuming that you want your progress counter to show something progressing, not just go from 0% to 100% in about 1ms and no one ever sees anything other than 100% anyways... But I have been assuming a few things wrong when it comes to beginner programmers, so I guess this could be one of those. Have you actually tried something - and if so, what was the effect? I'm not going to write the code for you - that's not how YOU learn. – Mats Petersson Dec 26 '12 at 16:33
  • Sorry, I just didn't really understand. How do I convert the "0%" for example to the "25%" using your << '/r' << Flush? I assume I'm supposed to write something like cout<< "0%" << '/r' << Flush; cout<<"25%"; ? – user1930233 Dec 26 '12 at 16:38
  • Yes, you need multiple cout statements. Whether that's part of a loop, several individual ones, etc depends on what your intentions are - which is still not clear. The code you have shown above, if you make my modifications, could just be replaced with `cout << "100%";` since there is nothing taking time in between, so it will go very quickly onto the screen. – Mats Petersson Dec 26 '12 at 16:40
  • Yes, but you can Sleep(1000) in between. I'm just wondering what people use when they want to show a loading bar in the console window, it's usually in percentages. – user1930233 Dec 26 '12 at 16:45
  • Yes, but you show a loading bar BECAUSE you are _DOING_ something! – Mats Petersson Dec 26 '12 at 16:49
  • I'm not really "doing" anything, I'm just wondering for future reference. Thanks for answering my questions though. – user1930233 Dec 26 '12 at 16:51
  • @user1930233 Do you understand the difference between `/` & `\ `. His answer uses `\r` but you keep referring to it as `/r`? – user93353 Dec 27 '12 at 07:10