I thought that, if cin
enters an error state, the variable it is streaming into remains unchanged. However, the following seems to be a counterexample:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter int: ";
int i = 5;
cin >> i;
if(cin.fail()) cout << "failed \n";
cout << "You entered: " << i << "\n";
}
Running:
Enter int: g
failed
You entered: 0
Where did I go oh so wrong?