loading...

std::cout is not printing in console/terminal

lucpattyn profile image Mukit, Ataul ・1 min read

Suppose in a C++ program, you are doing this but nothing appears in the console or terminal window.

std::cout << "running 1 ..";

This may happen because std::cout is writing to output buffer which is waiting to be flushed.
If no flushing occurs nothing will print.

So you may have to flush the buffer manually by doing the following:

        std::cout.flush();

However, if you call std::endl at the end, then it will invoke the flush, so you wouldn't have to call it manually.

If you write std::cout << "running 1 .." << std::endl;

You would see output is now showing in the console or terminal window.

Thanks https://dev.to/cnskn for pointing it out.

Posted on by:

lucpattyn profile

Mukit, Ataul

@lucpattyn

I consider myself as an enthusiast in learning new things everyday, about our history and future as well as the present. My interests: Ancient History, Comparative Religion, Science and Tech, C++

Discussion

markdown guide
 

Hello,
Never experience this problem, but what is even more odd is that endl do call flush as well (check en.cppreference.com/w/cpp/io/manip...)

I'm wandering what compiler/std/OS you used?

 

Mac OSX and gcc/g++ compiler.

 

Thanks, i actually forgot the endl in the program :) .

then used cout.flush() and when it started working i added the endl again.

Thanks for correcting this tip :)