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>
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?
source share