I'm trying to validate user input that user must enter number and it must be greater than 0, the validation of only numbers I got it working; however, I can't seem to incorporate the validation of greater than 0
1 2 3 4 5 6 7 8 9 10 11 12
float income;
cout << "How much did you earn last year: ";
//validating imput for income
while(!(cin >> income))
{
char ch;
cin.clear();
cout << "Sorry, number must be biger than \"0\" \n"
<< "How much did you make last year: ";
while(cin.get(ch) && ch != '\n');
}
float income;
cout << "How much did you earn last year: ";
cin >> income;
//validating imput for income
while(income == 0) //Here was the problem
{
cout << "Sorry, number must be biger than \"0\" \n";
cout << "How much did you make last year: ";
cin >> income; //This is just how I would do this.
}
I think that should fix it. I don't know what you had going on in that while condition. I'm not sure what exactly you're trying to do either, the full code would help.
Neither version makes sense. Zexanima's version because he's using the assignment operator ('=') instead of the comparator ('==') (or in the case: unequality comparator '!=').