开发者

how do I check if input from cin is a double? [duplicate]

开发者 https://www.devze.com 2023-01-07 13:32 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: How to validate numeric input C++
This question already has answers here: Closed 12 years ago.

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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消