0

I have a quick question for input verification. I have my loop, and it works for negative numbers, but when I input something like a + sign, it causes an infinite loop. Is there any way to fix it?

cout << "Input Quarter 1 Sales: " << endl;
    cin >> quarter1;

    while (quarter1 < 0)                                                                                    
    {
        cout << "Error! Sales Must be a Positive Number!" << endl;
        cout << "Please Input a Positive Number" << endl << endl;
        cin >> quarter1;
    }
Master Yoda
  • 4,334
  • 10
  • 43
  • 77
Lawlzes
  • 1
  • 3
  • Be careful when asking questions that dont meet the relevant tags required to get proper attention. Ive added the relevant c++ tag for you to garner attention. – Master Yoda Dec 11 '14 at 23:19
  • Make sure to specify what you are working on, what language, env, etc – tatigo Dec 11 '14 at 23:24
  • My bad, thank you. New student haha – Lawlzes Dec 11 '14 at 23:34
  • http://stackoverflow.com/questions/13281669/how-do-i-sanitise-cin – learnvst Dec 11 '14 at 23:38
  • To check if you get a valid integer from cin: http://stackoverflow.com/questions/2075898/good-input-validation-loop-using-cin-c. You can alter the code given in the link so that you only break out of the loop when it's a nonnegative number. – zachyee Dec 11 '14 at 23:45

0 Answers0