0

I am writing a program that uses a loop takes a while. I am outputing progress but currently am printing a lot of things one after another, for example:

1% 2% 3% etc...

I want to have this output in place and am trying to do so by using backspace to delete something already printed to screen and replacing it with something else. I thought I may do so using the '\b' control character but it does not seem to work. pop_back() also does not work since I am working with a double.

How can I get delete what is printed to screen?

I am using Xcode on MacBook Pro.

an example of what imagine my final code may look like is:

#include <iostream>
...
...


int main(){

  for(;;){

      double SomeDoubleValue = 1234;//this is value I want to display in place

      /*code that deletes whatever is printed to screen here*/

      cout << SomeDoubleValue << "%"; //Displaying my value as progress percentage;


    }

}

Is there another way to do this other than using '\b'?

  • https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ – Eljay Sep 08 '20 at 03:38
  • Does [this](https://stackoverflow.com/questions/14043148/how-to-change-text-while-running-the-console) or [this](https://stackoverflow.com/questions/11271362/how-to-delete-printed-characters-from-command-line-in-c) answer your question? There are related questions mentioned there, too – 89f3a1c Sep 08 '20 at 03:40
  • 1
    Does this answer your question? [Erasing using backspace control character](https://stackoverflow.com/questions/12765297/erasing-using-backspace-control-character) – Human-Compiler Sep 08 '20 at 03:41
  • I have been through all those recommended pages and still cannot get it to work. – Bogdan Linchuk Sep 08 '20 at 03:56

1 Answers1

1

Using \b should work, but I usually find it easier and more convenient to use \r instead:

for (int i=0; i<100; i++)
    std::cout << '\r' << i/100.0 << std::flush;

One note though: if a later output is narrower than a previous one, this won't (automatically) overwrite the larger part. For example, if you modify the previous loop write out a 1 at the end:

for (int i=0; i<101; i++)
    std::cout << '\r' << i/100.0 << std::flush;

...the 1 will be written out without anything after the decimal point, and only overwrite the 0 that was at the beginning of the previous 0.99, so the final display will show as 1.99 instead of 1.00. To prevent that, you can (for one possibility) set fixed format, and a specified precision and width:

for (int i=0; i<101; i++)
    std::cout << '\r' << std::fixed << std::setw(3) << std::setprecision(2) << i/100.0 << std::flush;

(And yes, even after all these years, the verbosity of this syntax still bothers me a bit; I look forward to std::format).

Of course, if you're printing out increasing integers, you don't run into this particular problem.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • That doesn't work for me. It still prints successively and does not delete anything. – Bogdan Linchuk Sep 08 '20 at 03:56
  • 1
    @BogdanLinchuk: I guess that's not surprising. MacOS (<= 9.x) used to use `\r` as its line-ending character, and they've apparently broken their current shell to do the same. Almost the only reasonable way to fix this it to replace Apple's broken terminal emulator with one that isn't broken. – Jerry Coffin Sep 08 '20 at 04:08