c++中的整数输入验证

Integer Input Validation in C++

本文关键字:输入 验证 整数 c++      更新时间:2023-10-16

我有一个关于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。更简单:在你的情况下,它会尝试将你传递到流中的内容做成整数,并使数据成为"整型"。