c++ 中 while 循环的意外行为

Unexpected behavior of while loop in c++

本文关键字:意外 循环 while c++      更新时间:2023-10-16

做了这样的事情:

int main()
{
while (true)
{
std::cout << "Enter a number between one and nine. n";
int oneandnine;
std::cin >> oneandnine;
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(100, 'n');
std::cout << "INVALID UNPUT!n";
}else if (oneandnine <= 9 &&  oneandnine >= 1)
{
break;
}else
{
std::cout << "INVALID UNPUT!n";
}
}
return 0;
}

当输入提供类似这样的456aihdb时,得到这样的东西:

INVALID UNPUT!
Enter a number between one and nine.
INVALID UNPUT!
Enter a number between one and nine.

为什么它会像这样循环两次? 是因为当456被丢弃而其余的aihdb没有被丢弃时,这会导致它再次循环并跳过cin输入?

它完全符合您的想象。

fail标志不会立即设置,而是格式化输入运算符将整数456读入oneandnine,但不设置fail标志,因为它是有效的整数值。这导致else案例执行,因为std::cin.fail()是假的,oneandnine不在19之间。

下一次迭代时,读取无效输入,将设置fail标志,导致第二个错误输出。


处理验证的一种常见方法是将整行读取到一个字符串中,将该字符串放入std::istringstream并使用它来尝试解析输入:

if (!std::getline(std::cin, line))
{
// Failure of some kind, could be EOF or something else
// Probably best not to continue in this case
}
std::istringstream iss(line);
if (!(iss >> oneandnine))
{
// Invalid input, report it as such
}
if (oneandnine < 1 || oneandnine > 9)
{
// Invalid number, report it as such
}
// Correct input, continue with program

请注意,上述代码将认为6abc等输入有效。6将被提取到oneandnine中,abc部分将被静默丢弃。如果不需要,还有其他解析方法(例如std::stoistd::strtol,如果不需要例外(。这样做而不是>>提取,但上面的其余代码应该没问题。

std::istreamoperator >>不会整行读取。它会读取直到找到无效字符或空格,如果在无效字符之前找到有效字符,则读取操作将成功,并且无效字符将保留在流中。

在您的示例中,第一次迭代成功读取456并将aihdb保留在流中。这将失败您的范围检查,然后第二次迭代尝试读取剩余字符,但由于第一个字符不是数字而失败。

如果要读取整行,请使用std::getline则将整行解析为一个数字。例如:

#include <iostream>
#include <string>
using std::cout;
int main()
{
while (true)
{
std::cout << "Enter a number between one and nine. n";
std::string line;
std::getline(std::cin, line);
int oneandnine;
size_t pos;
try
{
oneandnine = std::stoi(line, &pos);
}
catch ( std::exception& )
{
oneandnine = -1;
}
if (pos != line.size() || oneandnine > 9 || oneandnine < 1)
{
std::cout << "INVALID INPUT!n";
}
else
{
break;
}
}
return 0;
}