Search code examples
c++coutgetchar

std::cout output not printed till program ends


I have a few std::cout statements and few of them do not print output to console till program ends. But it i put getchar(), it starts printing output. Why does it happen, can someone please explain?


Solution

  • The issue may be because you are not trying to flush your output. You can try like this:

    std::cout << "some text" << std::flush;
    

    or like

    std::cout << "some text" << std::endl;
    

    The standard output is buffered and on newline the buffer is flushed.