Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there guys.

Superb weird issue I got with c++... hope anyone can help me out understanding root cause...
The code:
for(int i = 0; i < 10; i++) {
    std::cout << i << " ";
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}


Now - the thing is that if I run this in windows - the output comes as expected - every second a number is printed into console. And - when trying this on ubuntu machine it shows nothing and after 10 seconds prints all numbers instantly.

Any idea why it does not print the output every second as in windows?

What I have tried:

Tried building and running this in CodeLite with and without integrated terminal. Tried compiling and running using simple terminal manually (tried c++11 and c++14).
Posted
Updated 27-Jul-18 6:49am

1 solution

Try adding a std::flush to the cout line.
std::cout << i << " " << std::flush;
 
Share this answer
 
Comments
MK-Gii 27-Jul-18 12:56pm    
Fantastic. Thank you:) That was fast, simple and graceful :)
jeron1 27-Jul-18 13:13pm    
You're welcome. Doing a quick search for 'stdout buffering' will help you with details of why it was doing what it was doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900