In C, prompting the user for input followed the pattern where you issued a command to print a prompt message, and then a command to input a value.
In order for this process to work properly, you need to ensure the prompt message is actually displayed. Environments were configured so that this was automatic:
- In a DOS (or windows command line) environment,
STDOUT
is unbuffered, so the print command displays the message immediately.
- In a *NIX environment,
STDOUT
is line buffered. So, if you followed the *NIX style of including a newline in your prompt message so that the user enters input on the next line, your prompt would display automatically before input.
The tie feature makes this behavior of automatic flushing part of the way in which the iostream library works, rather than a product of convention. (although be aware that the automatic flushing only happens when the program asks the system to obtain more input, so there are edge cases where cin >> c
won't actually flush)
However! By default the iostream library is synchronized with the stdio library. In practice, this means the iostream doesn't do any buffering at all; it simply forward the data over to the underlying C library.
So, this means you're seeing what you what you would normally see if you wrote a similar C program. If you are in a windows command line environment, output wouldn't be buffered, and so you see the prompt before entering input.
To see the native C++ behavior, you need to turn off synchronization by running std::cout.sync_with_stdio(false);
at the start of your program, as indicated in the other answer.
If you do that, then the cout
statement won't flush the output buffer (unless the buffer is very tiny). And since you've removed the tie, the cin
statement won't flush it either. So, you'll get the result where you have to enter user input before you see the prompt.