std::cout not printing the value with out endl (newline specifier)

前端 未结 1 1560
半阙折子戏
半阙折子戏 2020-12-21 17:07

Below is my c++ program to multiply two strings (Integers in strings) and produce the integer result in string. I believe it is due to cout flush problem. Can someone explai

1条回答
  •  醉梦人生
    2020-12-21 18:06

    You should notice that std::endl includes flushing the output stream. As from the reference documentation:

    Inserts a newline character into the output sequence os and flushes it as if by calling os.put(os.widen('\n')) followed by os.flush().

    So if you flush cout after printing in your example

    cout << multiply("0", "0");
    cout.put(cout.widen('\n'));
    cout.flush();
    

    you would see the result printed Live Demo.

    For more information about flushing and what it actually means for buffered output read here please std::ostream::flush().

    0 讨论(0)
提交回复
热议问题