Cout not printing inside loops

Hi everyone,

I've noticed on several occasions that for some reason, "cout" doesn't print its output on the screen until it encounters "endl". This is very unfortunate, as sometimes, I want to print information inside a loop, without necessarily putting each bit of information on a new line.

Another example would be something like this. For instance, I would like to print at the beginning of the loop that the program is going to go through it, followed by 'Done' if it has fininshed going through the loop without errors:

1
2
3
4
5
cout << "[info] Starting loop... ";
for (int i=0; i<max_i; i++) {
    //loop statements
    }
cout << "Done." << endl;


But if I try this, then it just prints the whole thing after encountering "endl" after finishing the loop, instead of printing '[info] Starting loop...', going through the loop, and appending 'Done.' after finishing.

So, my question is: Is there a way around this, so I can get this information on my screen at the appropiate times?
Yes - look at std::flush. It forces the buffer through, and really std::endl is just << '\n' << std::flush;.
Great! That did the trick, thanks!
Topic archived. No new replies allowed.