You quoted the answer:
Output buffers can be explicitly flushed to cause the buffer to be written.
That is, you may need to "clear" the output to make it be written to the base stream (which may be a file or terminal in the examples given).
Normally stdout / cout is buffered by a line: the output is not sent to the OS until you write a new line or explicitly clear the buffer. The advantage is that something like std::cout << "Mouse moved (" << px << ", " << py << ")" << endl calls only one entry in the base "file" instead six, which is much better for performance. The disadvantage is that such a code:
for (int i = 0; i < 5; i++) { std::cout << "."; sleep(1);
will output ..... immediately (for an accurate implementation of sleep , see this question ). In such cases, you will need an extra << std::flush to ensure output is displayed.
Reading the cin dumps therefore you do not need an explicit reset for this:
std::string colour; std::cout << "Enter your favourite colour: "; std::cin >> colour;
tc. Feb 23 '13 at 16:50 2013-02-23 16:50
source share