tags:

views:

51

answers:

2

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.

+3  A: 

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.

sbi
+3  A: 

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.

anon
What if i do wanna maintain the 'Enter your name:" in the buffer without displaying it and meantime i wanna read?
yapkm01
@yapkm01 Like I said - untie them. It seems unlikely you actually do want to do this, however.
anon
cool Thx a zillion :)
yapkm01
@yapkm01: If you think this answers your question best, you should accept it.
sbi
@sbi Another better answer might come along from someone in a different timezone. Please do not pressure people (especially on my behalf) to accept.
anon
@Neil: As I understand it, you can always change the accepted answer later. If an answer fits and answers the question, why not accept it? Anyway, I wasn't trying to pressure. I just thought that someone with a rep of 6 might not know the rules well enough and could live with being hinted at them.
sbi