yapkm01
yapkm01

Reputation: 3775

Flushing of cout prior to reading input using cin .. why?

Why does cout has to be flushed before cin starts reading? Aren't they of different buffer? I can have reading of input into a buffer while same time putting it on output buffer (before flushing) .. 2 different buffers. I am confused here.

Upvotes: 2

Views: 998

Answers (2)

anon
anon

Reputation:

It doesn't have to be flushed. By default the streams are tied together so that when you do things like:

cout << "Enter your name:";
cin >> name;

the prompt appears before the input starts - this is just a convenience feature. However, you can untie them:

cin.tie( static_cast<ostream*>(0) );

following which cout will not (necessarily) be flushed before input is performed on cin.

Upvotes: 8

sbi
sbi

Reputation: 224079

The canonical example is this:

 std::cout << "Enter your name: ";
 std::string name;
 std::cin >> name;

You do want to see the prompt before the input, that's why those two streams are tied together.

Upvotes: 7

Related Questions