开发者

Question on combination of >>, << operators & cin and cout objects

开发者 https://www.devze.com 2023-03-03 00:05 出处:网络
char word[10]; int n=5; while(n--) { cin>>word; cout<<n<<\" \"<<word<<\" \";
char word[10];
int n=5;
while(n--)
{
  cin>>word;
  cout<<n<<" "<<word<<" ";
}

Output:

ABC DEF GHI JKL MNO
4 ABC 3 DEF 2 GHI 1 JKL 0 MNO

Now, my question is what happens when input buffer encounters a blankspace(' ')? It is seen that n is being decremented after every white space but the cout << word does not display anything on screen.

I am confused as i think that the output should be displayed as soon as one word is input. Eg.

 ABC 4 ABC DEF 3 DE开发者_如何学PythonF GHI 2 GHI JKL 1 JKL MNO 0 MNO


Not sure I understand your question, but if I'm reading you right: The stream extraction operator reads until it encounters whitespace, and then consumes the whitespace. You don't get a new word consisting of just the whitespace characters.


A few minutes later: I went back and re-read again, and now I think I understand what you're asking: the two streams are not synchronized, so the input and output can't be interleaved in the way you suggest.


cin read strings separated by space but space are discarded in the process


Try doing

cout << flush;

Or

cout << endl;

(inside the while)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号