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

pacumming
pacumming used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
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

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

Author

Commented:
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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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.
Top Expert 2012
Commented:
Argh, sorry, make that
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;
      ss.clear(); // clear state

      continue; // try again
    }

    break; // all valid, end loop

  } ;

  return d;
}

Open in new window

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.

Author

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial