int x; // intitialize x but don't immediately prompt for input. This makes the program more user friendly
cout << "Enter a year to check if it's a leap year: " << endl;
cin >> x;
while (cin.fail()) // check for input error
{
cin.clear();
cin.ignore();
cout << "You entered an incorrect value. Try again" << endl;
cin >> x;
}
I'm having trouble understanding how fail states work in c++. What I want to happen is if the user inputs anything that contains something besides a number, It will clear the buffer and prompt for another input. My code doesn't seem the to do that. If I enter a something like say, r4, then the rest of my code (which works just fine so I didn't show it below) will run with the four. It seems like the loop is activated at some point. The cout string is still printed. It's just that It doesn't give me to opportunity to re-input and continue checking.