c++中的整数输入验证
Integer Input Validation in C++
我有一个关于c++输入验证的问题。这是我的代码,我有麻烦:
#include <iostream>
using namespace std;
int main()
{
int in;
while(true)
{
if(cin >> in)
{
if(in < 0 || in > 2)
{
cout << "Invalid input. Number needs to be 0, 1, or 2.";
cin.clear();
while(cin.get() != 'n');
cin.ignore();
}
else
{
cout << "Output: " << in;
break;
}
}
else
{
cout << "Invalid input. Please enter a number.";
cin.clear();
while(cin.get() != 'n');
cin.ignore();
}
}
}
这段代码工作正常,除非在一行中有两个无效条目,第二个输入形式为'12hfhd'。然后它接受这个作为输入,我不知道为什么。我已经搜索了SO,并发现了一堆关于输入验证的问题,但似乎找不到任何关于他们的代码接受某些输入。
主要问题是,当使用>>
操作符从std::cin
请求int
时,将转换输入开头的一系列数字字符。例子:
-
2
将转化为2
-
75$
将转换为75
-
12asdfgh
将转换为12
-
hello,world
将转换为0
,因为第一个字符已经不是数字
最好的办法是使用一些char
操作:
int getNumber() {
char input;
std::cin >> input;
if(input > '2' || input < '0') { // yes, chars act like ASCII numbers
// handle this problem
return -1; //error
} else {
return int(input - '0'); // input's code - '0''s code = number
}
}
在处理用户输入时,我会使用以下方法:
string l;
if(!getline(cin, l))
/* handle error that no input could be read at all */
try
{
int const res = boost::lexical_cast<int>(l);
/* numerically validate input here */
return res;
}
catch(exception const&)
{
/* handle parsing error here */
}
换句话说,读取一行,然后使用Boost的lexical_cast()
函数模板解析并验证它。请注意,如果有人从文件中读取输入(例如通过shell重定向),则会发生第一个错误getline()
失败,但这也可以通过某些按键来实现,具体取决于shell。此状态无法恢复,因此提示不同的答案将导致无限循环。
如果您查看>>提取操作符的文档,例如:
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/你会注意到下面的引语:
(1)算术类型从流中按顺序提取和解析字符,将其解释为正确类型的值的表示;存储为val的值
这实际上意味着程序将尝试按照右值指定的格式处理传递的所有数据,在这里是int。更简单:在你的情况下,它会尝试将你传递到流中的内容做成整数,并使数据成为"整型"。
相关文章:
- 正在尝试了解输入验证循环
- 如何在C++中检查2D数组中负值的输入验证
- cin 的十进制输入验证?
- 输入验证以筛选出字符、字符串和一系列整数
- 输入验证将数字放入向量中
- 在C++中使用字符数组进行输入验证
- 在C++中使用字符数组进行用户输入验证
- C++ 中字符数组的输入验证
- 检查节点的输入验证的整数
- 如何让我的程序在输入验证的基础上读取素数
- C++ 用户输入验证无法正常工作
- 程序终止,而不是继续整数输入验证循环
- 使用REGEX进行电子邮件输入验证C
- C++:输入验证器函数
- 将用户输入验证为整数:用户必须两次输入输入
- C 输入验证
- C :对输入验证函数进行编程的性能影响可以对其进行编程,而每次都可以将其编程
- 使用模板函数进行通用输入验证
- 为什么用于输入验证的 while 循环要么完全跳过循环,要么不考虑错误的输入
- 仅 C++ 字母和空格输入验证