The stream std::cout
is tied to std::cin
by default: the stream pointed to by stream.tie()
is flushed prior to every properly implement input operation. Unless you changed the stream tied to std::cin
, there is no need to flush std::cout
before using std::cin
as it will be done implicitly.
The actual logic to flush the stream happens when constructing a std::istream::sentry
with an input stream: when the input stream isn't in failure state, the stream pointed to by stream.tie()
is flushed. Of course, this assumes that the input operators look something like this:
std::istream& operator>> (std::istream& in, T& value) {
std::istream::sentry cerberos(in);
if (sentry) {
// read the value
}
return in;
}
The standard stream operations are implemented this way. When user's input operations are not implemented in this style and do their input directly using the stream buffer, the flush won't happen. The error is, of course, in the input operators.