如何在 C++ 中将用户输入验证为双精度值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3273993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do I validate user input as a double in C++?
提问by Hristo
How would I check if the input is really a double?
我将如何检查输入是否真的是双重的?
double x;
while (1) {
cout << '>';
if (cin >> x) {
// valid number
break;
} else {
// not a valid number
cout << "Invalid Input! Please input a numerical value." << endl;
}
}
//do other stuff...
The above code infinitely outputs the Invalid Input!
statement, so its not prompting for another input. I want to prompt for the input, check if it is legitimate... if its a double, go on... if it is NOT a double, prompt again.
上面的代码无限输出Invalid Input!
语句,所以它不会提示另一个输入。我想提示输入,检查它是否合法......如果它是一个双重的,继续......如果它不是双重的,再次提示。
Any ideas?
有任何想法吗?
采纳答案by casablanca
Try this:
尝试这个:
while (1) {
if (cin >> x) {
// valid number
break;
} else {
// not a valid number
cout << "Invalid Input! Please input a numerical value." << endl;
cin.clear();
while (cin.get() != '\n') ; // empty loop
}
}
This basically clears the error state, then reads and discards everything that was entered on the previous line.
这基本上清除了错误状态,然后读取并丢弃在前一行输入的所有内容。
回答by Ben Voigt
failbit
will be set after using an extraction operator if there was a parse error, there are a couple simple test functions good
and fail
you can check. They are exactly the opposite of each other because they handle eofbit
differently, but that's not an issue in this example.
failbit
如果出现解析错误,将在使用提取运算符后设置,有几个简单的测试函数good
,fail
您可以检查。它们完全相反,因为它们的处理方式eofbit
不同,但这在本示例中不是问题。
Then, you have to clear failbit
before trying again.
然后,您必须先清除,failbit
然后再重试。
As casablanca says, you also have to discard the non-numeric data still left in the input buffer.
正如卡萨布兰卡所说,您还必须丢弃仍留在输入缓冲区中的非数字数据。
So:
所以:
double x;
while (1) {
cout << '>';
cin >> x;
if (cin.good())
// valid number
break;
} else {
// not a valid number
cout << "Invalid Input! Please input a numerical value." << endl;
cin.clear();
cin.ignore(100000, '\n');
}
}
//do other stuff...
回答by cpx
One way is to check for floating number equality.
一种方法是检查浮点数是否相等。
double x;
while (1) {
cout << '>';
cin >> x;
if (x != int(x)) {
// valid number
break;
} else {
// not a valid number
cout << "Invalid Input! Please input a numerical value." << endl;
}
}
回答by Notinlist
#include <iostream>
#include <string>
bool askForDouble(char const *question, double &ret)
{
using namespace std;
while(true)
{
cout << question << flush;
cin >> ret;
if(cin.good())
{
return true;
}
if(cin.eof())
{
return false;
}
// (cin.fail() || cin.bad()) is true here
cin.clear(); // clear state flags
string dummy;
cin >> dummy; // discard a word
}
}
int main()
{
double x;
if(askForDouble("Give me a floating point number! ",x))
{
std::cout << "The double of it is: " << (x*2) << std::endl;
} else
{
std::cerr << "END OF INPUT" << std::endl;
}
return 0;
}
回答by Denys Shabelnyk
bool is_double(double val)
{
bool answer;
double chk;
int double_equl = 0;
double strdouble = 0.0;
strdouble = val;
double_equl = (int)val;
chk = double_equl / strdouble;
if (chk == 1.00)
{
answer = false; // val is integer
return answer;
} else {
answer = true; // val is double
return answer;
}
}
回答by mmontoya
I would use:
我会用:
double x;
while (!(std::cin >> x)) {
std::cin.clear();
std::cin.ignore(2147483647, '\n');
std::cout << "Error.\n";
}
or
或者
double x;
while ((std::cout << "> ") && !(std::cin >> x)) {
std::cin.clear();
std::cin.ignore(2147483647, '\n');
std::cout << "Error.\n";
}