0

I implemented a solution from this question, but it's a little buggy. This only seems to work if there are no spaces in input. Do I need some solution using getline instead? Thanks!

void numericValidation(double input)
{
        while(cin.fail())
        {
            cout << "Invalid Input. Please input a numerical value above zero." << endl;
            cin.clear();
            while(cin.get() != '\n' );
            cin >> input;
        }
}

double test;
cin >> test;
numericValidation(test); // Now I type: 1 2345 in the console (including the space)
// no error message is sent

cout << test << endl; // this prints 1
Community
  • 1
  • 1
ch-pub
  • 1,664
  • 6
  • 29
  • 52
  • `floor(foo) != ceil(foo) && foo > 0` (if it really has to be a double) – OMGtechy Apr 29 '14 at 21:37
  • The main problem I'm having is that if I actually enter "1 0.5 3 blah" in the console, input is actually set equal to 1, and this exception handling does not catch what the user typed in the console. Can I use getline instead somehow? – ch-pub Apr 29 '14 at 21:51

0 Answers0