Getting an output while running a program without using \n

Hello,

I was wondering if anyone could help me with a quick problem. I'm trying to run a program that takes a lot of time for computations. I've been wanting to use a clean way to show that the program is still running. e.g. a period "." for every few cycles, and/or that rotating slash "|" -> "/" -> "-" -> "\" that you see in old DOS programs. The thing is, the only way output seems to come out is when I use the new line "\n" escape sequence. And that gives me lines of output that I don't want. Nothing else seems to work in the program. Can someone help?
For the | -> / -> - -> \
You can use system("CLS") which will clear the screen
Bad advice is bad.
1
2
3
4
5
6
7
8
char *busy="|/-\\";
char busyindex=0;
/*loop*/{
	//...
	printf("\b%c",busy[busyindex]);
	busyindex=(busyindex+1)%4;
	//...
}
Last edited on
Thanks for the suggestion, but I think you guys missed my point. Nothing prints during the loop when I do that. It will only show when the program gets to the next "\n" in the program. I'll give and example.

When I do:
1
2
3
4
5
6
/*loop 5 times*/{
	//...stuff that takes 5 seconds to process
	printf(".");
	//...
}
printf("Some other statement\n");

It only prints ".....Some other statement" all at once after 25 seconds. The periods don't come every 5 seconds.

The only thing I know of right now that flushes the buffer onto the screen is "\n". Which I don't want.
Btw, I don't think this should make a difference, but I'm using cygwin with gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
stdout by default is line buffered which means the output is only flushed to the screen when a newline is output.

What you need is this:

cout << '.' << flush;

The flush says to force the output to be written to the screen immediately.

Awesome!! That worked perfectly. Thanks!
Topic archived. No new replies allowed.