I get an endless loop if the user inputs a character, however the do/while loop works just fine if the user inputs a negative number, what am I missing? The fail condition seems to be working, but it's not clearing out the string input I think.
//get user income
cout << "What is your income amount? (round to the nearest dollar) \n";
cin >> income;
if (cin.fail() || income < ZERO)
{
do
{
cin.clear();
cout << "Please enter a non-negative numerical value\n";
cin >> income;
}
while (cin.fail() || income < ZERO);
}
You don't show the definition of income. Assuming it is an int, the problem is that the non-numeric character is left in the input buffer. cin.clear() clears only the fail bits. It does not remove the unused alpha character from the input buffer. For that you need:
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.