Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Why does SFML cut off output before the main while loop?  (Read 2081 times)

0 Members and 2 Guests are viewing this topic.

declan

  • Newbie
  • *
  • Posts: 34
    • View Profile
Why does SFML cut off output before the main while loop?
« 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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Why does SFML cut off output before the main while loop?
« Reply #1 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.
Laurent Gomila - SFML developer

declan

  • Newbie
  • *
  • Posts: 34
    • View Profile
Why does SFML cut off output before the main while loop?
« Reply #2 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Why does SFML cut off output before the main while loop?
« Reply #3 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.
Laurent Gomila - SFML developer

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Why does SFML cut off output before the main while loop?
« Reply #4 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 .
Want to play movies in your SFML application? Check out sfeMovie!

 

anything