-
updating countdown
or rather countup. What I want to do is display on the screen a percentage of the work completed. If I just use cout it will write one percentage after the other. But what I want to do is something like what you see when you're formatting your Hard Drive that shows in the same place.
I've got a workaround for this but It's crap, limits a lot my possibilities and uses 100% of the resources.
Any ideas?
TIA
-
Tried something simple like gotoxy() then writing the percent?
-
Clearing the screen inbetween cout's would do it as well.
-
Actually, just repeatedly clearing the screen wouldn't be so great. Either you would have to redraw all the text that should be displayed on the screen or you would have nothing on the screen except the percent (most likely not an option). Aside from that, you may want the percent somewhere on the screen aside from (0, 0), such as on the bottom line or whatever.
-
>>Any ideas?
This might work for you :-)
Code:
#include <iostream>
#include <iomanip>
// Assume Windows or POSIX
#if defined(__WIN32__)
#include <windows.h>
#define sleep Sleep
#else
#include <unistd.h>
#define sleep usleep
#endif
using namespace std;
int main()
{
for (int i = 0; i <= 100; i++)
{
cout<<'\r'<< setw(3) << i <<'%'<<flush;
sleep(100);
}
}
-
Thanks a lot Cela, not sure why it works though doesn't seem like it should, maybe that carriage return...
Anyhow, it works great!
>>clearing the screen between cout's would do it as well
That's what I had but it uses most of the resources. On one program I was clearing the screen and re-writing everything in an infinite loop. Man, that took resources. 100% throughout.
-
>>not sure why it works though doesn't seem like it should, maybe that carriage return...
Yep, it's the carriage return. The cout statement returns to the beginning of the line and prints 4 characters, then the next call returns to the start and again prints 4 characters. So it looks like the counter is updating :-)