Link to home
Start Free TrialLog in
Avatar of pacumming
pacummingFlag for United States of America

asked on

C++ newbie - What is best way to validate input if numeric

Still working on a program and have searched the internet for various ways to test to see if input (DOS) is numeric. I see there are numerous ways and not sure how to proceed.
Values must remain as DOUBLE. Want user to have to re-input a value if they enter any non-numeric input. I guess they could enter positive and negative numbers as well as decimal. Need warning message if value is not numeric. Am using Dev C++ compiler
Thanks for the help
PC
volfunc.cpp
Avatar of jkr
jkr
Flag of Germany image

The simples thing would be to create a function that prompts for the input, reads that as a string and then converts that to 'double' while checking for the validity, e.g.

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

double read_double(const string& prompt) {

  double d;
  string line;
  stringstream ss;

  while (1) {

    cout << prompt;

    getline(cin,line); // read input
    ss.str(line);

    ss >> d;

    if (ss.fail()) { // check conversion

      cout << "Input invalid. Please enter a valid number." << endl;
      continue; // try again
    }

    break; // all valid, end loop

  } ;

  return d;
}

//...

int main()
{                                 
   double area, height, volume;                    // declare variables
   area = read_double("Please enter the Area of the Square-based pyramid:");       // requests users input

                                                                       
   //...

    return 0;
}

Open in new window

Avatar of phoffric
phoffric

Here is a simple approach from:
http://stackoverflow.com/questions/784563/c-check-whether-is-number-is-int-float

Just make a readDouble function and loop until test passes.
if( !(cin >> height)) {
   cout << "Bad height" << endl;
}

Open in new window

Avatar of pacumming

ASKER

Tanks,
seems to be a problem. If you enter perhaps 1111 it works fine. Great.
But it accepts 1a23 which it should not.

If you enter something invalid like   a123   or something invalid, you can then never enter a valid number since it continues to cause a fail.

Can you put something in there that says input valid if it is valid? THen I will try to incorporate this into my code.

Thanks
Peter
1a23 will give 1.0 for the first double, and then fail for the 2nd double. If you want a whitespace to be the token delimiter, then cin >> string, and then check to see if the string contains the right format for a floating point number.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I didn't mean cin >> string literally; I meant cin >> stringName.
I thought you wanted an input of 1a23 to fail. It does not fail, but at least it throws away the a23. If you start to prompt for more than one double, then you will need to adjust.
I actually do want 1a23 to fail and it still does not.  Only numeric +- and decimal should pass.
I opened up another question for you

Thanks
PC