Problem of using cin twice

前端 未结 6 1035
攒了一身酷
攒了一身酷 2020-12-01 13:27

Here is the code:

string str;
cin>>str;
cout<<\"first input:\"<

        
6条回答
  •  庸人自扰
    2020-12-01 14:08

    The \n is left in the input stream as per the way operator>> is defined for std::string. The std::string is filled with characters from the input stream until a whitespace character is found (in this case \n), at which point the filling stops and the whitespace is now the next character in the input stream.

    You can also remove the \n by calling cin.get() immediately after cin>>str. There are many, many different ways of skinning this particular I/O cat, however. (Perhaps a good question in and of itself?)

提交回复
热议问题