I have used boost's progress bar (it outputs ***** from 0 to 100% complete) to track the progress of long loops. It would also be nice to be able to see a counter along with that, like if I run 1000 tests, the *'s will go from 0 to 100%, but I would also like to see
4 tests pass
5 tests pass

etc. without having a new line for each, ie the 5 would simply replace the 4, so the progress bar could remain visible while I am updated about the number of tests that have passed.

Has anyone seen anything that does something like this?

Thanks,
Dave

>>Has anyone seen anything that does something like this?
just add '\r' to the print statement

int counter;
for(counter = 0; counter < 1000; counter++)
{
    cout << "\rcounter";
    Sleep(500); // 1/2 second delay
}

hmm, in trying that, I discovered this:

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
	for(int counter = 1000; counter < 10000; counter++)
	{
		//cout << "hi" << endl; // this works
		cout << "hi"; //this doesn't work
		sleep(1);
	}
return 0;
}

If I put an endl at the end of the output, every 1 second I see a new line that says "hi". If I don't put the endl, I would expect to see

hihihihihihihi

but instead I see nothing! Why would that be? I was also seeing nothing with the \r, but I bet it's the same reason.

Thanks,
Dave

Member Avatar for iamthwee

would also like to see
4 tests pass
5 tests pass

etc. without having a new line for each, ie the 5 would simply replace the 4, so the progress bar could remain visible while I am updated about the number of tests that have passed.

I see this as one of those clearing the console windows questions...

hmm, in trying that, I discovered this:
but instead I see nothing! Why would that be? I was also seeing nothing with the \r, but I bet it's the same reason.

Thanks,
Dave

You wrote the wrong test program. Assuming you are writing this under MS-Windows os. If *nix then delete windows.h and replace Sleep() with sleep().

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    int counter;
	for(counter = 1000; counter < 10000; counter++)
	{
		cout << "\rhi " << counter; cout.flush();
		Sleep(250); // 1/4 second sleep
	}
    cout << "\ncounter = " << counter << "\n";
return 0;
}
commented: Ancient Dragon has all the answers! +3

bah cin.flush().... I always forget that! I guess I don't understand why it isn't done automatically.

That \r is exactly what I was looking for, but now that I think about it I probably can't do exactly what I wanted. As I mentioned the boost progress bar writes
*
then replaces it with
**
then
***
etc. , so I would have to allow it to keep writing *'s in a line at the same time as outputting my counter with \r, and that doesn't seem do-able.

Thanks,

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.