0

Possible Duplicates:
How to validate numeric input C++
how do I validate user input as a double in C++?

I need to get input from the command line and check if it is a valid number... storing it as a double. If the input is invalid, I need to keep asking for a number.

double x;
cout << '>';
cin >> x;


while (/*x is invalid*/){
 cout << "Invalid Input! Please input a number." << endl;
 cout << '>';
 cin >> x;
}

So how do I check if it is valid?

Community
  • 1
  • 1
Nasko
  • 9
  • 1
  • 1
  • 2
  • possible dupe of http://stackoverflow.com/questions/3273993/how-do-i-validate-user-input-as-a-double-in-c – rubenvb Jul 18 '10 at 08:06

2 Answers2

2

parse the string and check if there are numbers and 0 or one dot. If these rules aren't met, you just print an error message.

Quonux
  • 2,975
  • 1
  • 24
  • 32
  • how do I do this... I've never programmed before. – Nasko Jul 17 '10 at 22:23
  • oh in this case, just look here on stackoverflow for "C++ Tutorial" for example: http://stackoverflow.com/questions/909323/what-are-good-online-resources-or-tutorials-to-learn-c or use google or something. – Quonux Jul 17 '10 at 22:38
2

See "How to validate numeric input in C++" - already asked and answered on the site.

Community
  • 1
  • 1
Will A
  • 24,780
  • 5
  • 50
  • 61