简体   繁体   中英

cout does not print even with time delay

I expect cout to print "hello" and two seconds later " world".

int t = time( NULL );

std::cout << "hello";

while( time(NULL) < (t + 2) );

std::cout << " world";

But instead, cout prints noting to screen untill after two seconds later, then the program prints "hello world". Even if time delay increases like (t + 9) , it is the same result. I Am not familiar with this cout behavior.

But if I add std::endl at the first cout like so:

std::cout << "hello" << std::endl;
...

I get the expected result( "hello" and after two seconds later " world " ).

std::cout is usually buffered, meaning it may not output immediately unless you force it to. Try std::flush after your first output:

std::cout << "hello " << std::flush;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM