Clarification of the basic behavior of cin in C ++

I am curious why cin behaves as follows. I think I might have some misunderstandings regarding his behavior.

Consider this simple code. This code asks for input of some input, all of which are printed in the last statement.

 #include <iostream> #include <string> using namespace std; int main(int argc, char** argv) { cout << "Please enter your input: " ; int a=3, b=87; // initialized to some random integers string s = "Mary" ; // initialized to a random string cin >> a ; cin >> b ; getline(cin,s); cout << "You entered the following " << a << " " << b << " " << s << endl; return 0; } 

Now, if the input is 12 34 cat , the output is 12 34 cat This is expected.

However, if the input is cat 23 dog , the output is 0 87 Mary .

This is why I think this is unexpected:

cin >> a should fail, since cat cannot be converted to an integer. However, a is replaced by what I believe is the value of garbage.

Now, since the second input number is an integer 23, cin >> b must be done. However, this operation seems to fail, and b continues to retain its original meaning, unlike what happened with a .

Similarly, getline does not put the string <space>dog in string s which continues to retain its original Mary value.

My questions are as follows.

  • Does any cin operation fail if all subsequent cin operations fail using the >> operator or the getline function.

  • Why did the failure of the first cin operation change the value of a while the initial values โ€‹โ€‹of b and s remained unchanged?

+5
source share
3 answers
  • The failure of some cin operations indicates a failure of all subsequent cin operations using the โ†’ operator or the getline function.

Yes. Your code expects to read the input value exactly in that order

 cin >> a ; // 1st integer value cin >> b ; // 2nd integer value getline(cin,s); // string value 

Giving him input like

 cat 23 dog 

causes the fail() state to be set to cin when trying to read the value 1 int , and none of the following operator>>() calls will be executed.


cin >> a should fail, since cat cannot be converted to an integer. However, a is replaced by what I believe is the value of garbage.

This is not the meaning of garbage, but is clearly defined, see the reference quote below.

Now, since the second input number is an integer 23, cin >> b must be done. However, this operation seems to fail, and b continues to retain its original meaning, unlike what happened with a .

This assumption is incorrect, since the mentioned cin is in a fail() state at this point, and the analysis of another input is generally skipped.

You need to call clear() after each call to operator>>() to make sure that the input will be processed:

 cin >> a ; // 1st integer value cin.clear(); cin >> b ; // 2nd integer value cin.clear(); getline(cin,s); // string value 

  1. Why did the failure of the first cin operation change the value of a, while the initial values โ€‹โ€‹of b and s remained unchanged?

Since the link std::basic_istream::operator>>() says

"If the extraction fails, zero is written to the value and set to failbit . If the extraction results in a value that is too large or too small to match the value, std::numeric_limits<T>::max() or std::numeric_limits<T>::min() written std::numeric_limits<T>::min() and the failbit flag failbit set. (since C ++ 11) "

+5
source

Is it possible to fail any cin operation, subsequent cin operations using the โ†’ operator or the getline function.

Yes. Until you fix the error with cin.clear() . In addition, if extraction fails, the characters remain in the buffer, so if you try to read the same type again, it will not work again.

Why did the failure of the first cin operation change the value while the initial values โ€‹โ€‹of b and s remained unchanged?

Because (starting from C ++ 11) a change in the value to 0 is defined in case of unsuccessful extraction of a (valid) valid stream. Prior to C ++ 11, it would have remained unchanged. For a stream in an error state, the operation does nothing, therefore b and s not changed.

+9
source

You should use the cin.good() function or the abbreviated if(cin) notation as @AndyG pointed out to check the state of the cin object. the variable a is of type int , then how can you and why do you enter a string? Thus, it gives an unexpected output for the variable a .

+2
source

All Articles