SFML community forums

Help => General => Topic started by: declan on August 15, 2011, 10:30:44 pm

Title: Why does SFML cut off output before the main while loop?
Post by: declan on August 15, 2011, 10:30:44 pm
Hi guys, I've noticed something strange. I just want to output a few variables before the main while(App.IsOpened()) loop, but it seems like it doesn't display the last thing before the loop until something inside the loop outputs. For example, if I have:

Code: [Select]

cout << endl << "Vert angle before: " << twoLayers[0].vertical_tilt_angle;
cout << endl << "Horiz angle before: " << twoLayers[0].horizontal_tilt_angle;
while(App.IsOpened()){


It only displays the first line, until an event in the loop triggers another cout, at which point it outputs the second line, then the one from the event.

If I put in this:

Code: [Select]

cout << endl << "Vert angle before: " << twoLayers[0].vertical_tilt_angle;
cout << endl << "Horiz angle before: " << twoLayers[0].horizontal_tilt_angle;
cout << endl << "MOOOOOOOOOO";
while(App.IsOpened()){


It outputs both, but not the MOOOOOOO until, as before, something in the loop outputs.

I'm noticing this inside the loop as well, and it's a little annoying. Does anyone know the cause of it?

Thanks!
Title: Why does SFML cut off output before the main while loop?
Post by: Laurent on August 15, 2011, 10:38:44 pm
std::cout is buffered. Use std::endl or std::flush to force it to output its internal buffer to the console.
Title: Why does SFML cut off output before the main while loop?
Post by: declan on August 15, 2011, 10:41:32 pm
Quote from: "Laurent"
std::cout is buffered. Use std::endl or std::flush to force it to output its internal buffer to the console.


I don't really know what that means, but it worked!

Thank you, I am always baffled by how fast you respond to things here. It's awesome.
Title: Why does SFML cut off output before the main while loop?
Post by: Laurent on August 15, 2011, 11:06:22 pm
Quote
I don't really know what that means

Ok, sorry :D

When you write stuff to std::cout, it keeps it in an internal buffer, and decides to forward it to the console only later, when enough bytes are in the buffer.

So this kind of situation:
Code: [Select]
std::cout << "just a few characters";

... 2 hours later ...

std::cout << "more bytes";

... is very likely to output everything at once at the end.

To force std::cout to flush its contents to the console, std::flush must be used. Or std::endl, which is just '\n' + std::flush.
Title: Why does SFML cut off output before the main while loop?
Post by: Ceylo on August 16, 2011, 10:47:23 am
:o I didn't know for the '\n' + flush thing. I thought '\n' was enough to flush cout, and that '\n' was exactly the same as endl. Thanks :D .