validating user input

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');
    }

thanks
Last edited on
1
2
3
4
5
6
7
8
9
10
11
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.

[edit] Fixed what Gaminic pointed out.
Last edited on
Neither version makes sense. Zexanima's version because he's using the assignment operator ('=') instead of the comparator ('==') (or in the case: unequality comparator '!=').

Other than that, Zexanima's version should work.



yeah, the while condition is wrong, and besides I can still enter a character and that's one of the things I want to prevent
@Gaminic Thanks! I'm still a noob, so I make mistakes like that. xD
http://www.cplusplus.com/forum/beginner/18258/
Read the whole thread.

Hope this helps.
Topic archived. No new replies allowed.