11

Here is the code:

string str;
cin>>str;
cout<<"first input:"<<str<<endl;
getline(cin, str);
cout<<"line input:"<<str<<endl;

The result is that getline never pauses for user input, therefore the second output is always empty.

After spending some time on it, I realized after the first call "cin>>str", it seems '\n' is still stored in cin (using cin.peek() to check), which ends getline immediately. The solution will be adding one more line between the first usage and the second one: cin.ignore(numeric_limits::max(), '\n');

However, I still don't understand, why is '\n' left there after the first call? What does istream& operator>> really do?

gc .
  • 415
  • 4
  • 9
  • 18
  • 1
    Some more digging reveals how subtle the description can be: "Extraction ends when the **next** character is either a valid whitespace or a null character, or if the End-Of-File is reached." – gc . Mar 26 '10 at 20:36

6 Answers6

7

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?)

Conspicuous Compiler
  • 6,403
  • 1
  • 40
  • 52
fbrereto
  • 35,429
  • 19
  • 126
  • 178
  • 5
    A better way of getting rid of trailing whitespace characters would probably be to do `std::cin >> str >> std::ws;`. – sbi Mar 26 '10 at 17:47
  • 1
    Run some tests, realizing it is not the problem of how it is defined for std::string. In fact, operator>> leaves the newline in the stream whatever variable is following it. But thanks for starting the discussion of getting rid of white space! – gc . Mar 26 '10 at 19:47
2

By default, the stream insertion operator reads until it sees whitespace. Your first call isn't returning until it sees a space, tab, newline, etc. Then the next character needs to be consumed so that you can get to the next one.

jwismar
  • 12,164
  • 3
  • 32
  • 44
2

As others have said, th problem is that the newline is left from the first extraction. One solution I do is to discard all the left characters in the stream:

std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
null
  • 8,669
  • 16
  • 68
  • 98
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
2

I generally recommend only doing line-oriented input from std::cin. So, your code could look something like this:

string str;
int val;
// Read an entire line and parse an integer from it
{
    string line;
    getline(cin, line);
    istringstream iss(line);
    iss >> val;
}
cout<<"first input:"<<val<<endl;
getline(cin, str);
cout<<"line input:"<<str<<endl;

Be sure to add error checking too.

The getline-only approach avoids having to think about line buffering of the input, of clearing the input, etc. If you directly read something with >>, the input does not terminate if the user hits enter instead of inputting what is required, but instead continues until a token is input (this behavior is usually not wanted).

Tronic
  • 10,250
  • 2
  • 41
  • 53
0

Good writeup that explains some of the reasons why you are running into this issue, primarily due to the behavior of the input types and that you are mixing them

curtisk
  • 19,950
  • 4
  • 55
  • 71
0

Also was searching for most suitable solution. Implementation of this operator could produce problems. And not always is acceptable to read entire line, or not mix different types in one input line.

To solve problem, when you want to read some data from cin, and don't know if whitespaces was correctly extracted after last input operation, you can do like this:

std::string str;
std::cin >> std::ws >> str;

But you can't use this to clear trailing newline symbol after last input operation from cin to do not affect new input, because std::ws will consume all whitespaces and will not return control until first non-ws character or EOF will be found, so pressing enter will not finish input process. In this case should be used

 std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

which is more flexible.

P.S. If got errors with max() function such as "identifier expected", it could be caused by max macros defined in some header (for example, by Microsoft); this could be fixed by using

 #undef max
B-GangsteR
  • 2,534
  • 22
  • 34