My apologies for an inaccurate title, but I'm not sure what this is called exactly.
How would one print to the console a single, updating line?
For example, if I wanted to print a percent completion status every cycle but not flood the console with steams of text, how would I accomplish this? (What is this called? -- for future Googling)
Thank you!
Curses is only way to do this portably.
Have a look at this: http://code.google.com/p/tinycurses/wiki/basic1
There is no portable way to clean the screen though there is a simple way to return to the beginning of the line using \r
then overwriting what we wrote before. I am using Sleep
from Windows API
:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
for(int i = 1; i <= 10; i++)
{
std::cout << i*10 << '%';
std::cout.flush(); // see wintermute's comment
Sleep(1000);
std::cout << '\r';
}
}
The basic solution is to:
loop:
backspace (over written text)
write (without newline)
flush
wait and get updates
Alternatively, you could try a solution using the curses library - though I'm not sure whether this is quite what you're after. Curses provides your basic ascii graphics for text based GUIs (sometimes called a TUI).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With