C++ 如何让cin只取数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10828937/
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 to make cin take only numbers
提问by user1426900
Here is the code
这是代码
double enter_number()
{
double number;
while(1)
{
cin>>number;
if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input " << endl;
}
else
break;
cout<<"Try again"<<endl;
}
return number;
}
My problem is that when I enter something like 1x, then 1 is taken as input without noticing the character that is left out for another run. Is there any way how to make it work with any real number e.g. 1.8?
我的问题是,当我输入 1x 之类的内容时,将 1 作为输入,而不会注意到另一个运行时被遗漏的字符。有什么方法可以使它与任何实数(例如 1.8)一起使用?
采纳答案by Jesse Good
I would use std::getline
and std::string
to read the whole line and then only break out of the loop when you can convert the entire line to a double.
我会使用std::getline
和std::string
读取整行,然后只有在您可以将整行转换为双精度时才跳出循环。
#include <string>
#include <sstream>
int main()
{
std::string line;
double d;
while (std::getline(std::cin, line))
{
std::stringstream ss(line);
if (ss >> d)
{
if (ss.eof())
{ // Success
break;
}
}
std::cout << "Error!" << std::endl;
}
std::cout << "Finally: " << d << std::endl;
}
回答by Shiven
When cin encounters an input it can't properly read in to the variable specified (such as inputing a character into an integer variable), it goes into an error state and leaves the input in it's buffer.
当 cin 遇到无法正确读入指定变量的输入时(例如将字符输入到整数变量中),它会进入错误状态并将输入留在其缓冲区中。
You have to do several things to properly handle this scenario.
您必须做几件事才能正确处理这种情况。
- You have to test for this error state.
- You have to clear the error state.
- You have to either alternatively handle the input data that generated the error state, or flush it out and reprompt the user.
- 您必须测试此错误状态。
- 您必须清除错误状态。
- 您必须要么处理生成错误状态的输入数据,要么将其清除并重新提示用户。
The following code provides one of numerous methods of doing these three things.
以下代码提供了执行这三件事的众多方法之一。
#include<iostream>
#include<limits>
using namespace std;
int main()
{
cout << "Enter an int: ";
int x = 0;
while(!(cin >> x)){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Try again: ";
}
cout << "You enterd: " << x << endl;
}
You could just pass in some large value to cin.ignore like 1000 and it's likely to behave exactly the same for all practical purposes.
您可以将一些大值传递给 cin.ignore ,例如 1000,并且对于所有实际目的,它的行为可能完全相同。
You can also test cin after the input attempt and handle it that way, something like if(!cin){//clean up the error} .
您还可以在输入尝试后测试 cin 并以这种方式处理它,例如 if(!cin){//clean up the error} 。
Check out the istream reference for other member functions to handle stream state: http://cplusplus.com/reference/iostream/istream/
查看其他成员函数的 istream 参考以处理流状态:http: //cplusplus.com/reference/iostream/istream/