cin input (input is an int) 当我输入一个字母时,它不会打印不正确一次,而是打印正确一次然后在循环的其余部分使用 inc
cin input (input is an int) when I input a letter, instead of printing back incorrect once, it prints correct once then inc for the rest of the loop
我正在制作一个乘法练习程序。正如我的标题所说,如果我在控制台中输入一个字母而不是一个数字,它会 运行 关闭说第一个正确,但其余的不正确。即使你没有触摸键盘,它仍然会吐出错误。
ans = table * i;
std::cout << table << " * " << i << " =" << std::endl;
std::cin >> input;
if(input == ans)
{
std::cout << "Correct! " << ans << std::endl;
}
else
{
std::cout << "Incorrect, the answer was " << ans << std::endl;
input = 0;
ans = 0;
}
}
希望这能让您了解代码中发生的事情。这是输出。
Enter the table you'd like to learn.
5
Enter the amount of multiples
3
5 * 0 =
s
Correct! 0
5 * 1 =
Incorrect, the answer was 5
5 * 2 =
Incorrect, the answer was 10
5 * 3 =
Incorrect, the answer was 15
我该怎么做才能解决这个问题?感谢您的输入。
当您执行 cin >> input
并且它无法将输入解析为数字时,它会 sets cin
's failbit
,并且当设置 failbit
时它实际上不再读取任何数据直到你清除它。
您应该通过调用 cin.good()
, and try to clear them with cin.clear()
, but you shouldn't try to clear some errors (like EOF) 检查错误,而不是应该在错误发生时退出。
有多种方法可以构建各种测试,但是当使用 cin
(或通常使用任何输入函数)进行输入时,您必须考虑输入缓冲区中未读的所有字符。使用 cin
,您必须验证三个条件:
.eof()
(eofbit
) 已设置。输入结束,或者用户通过按 Ctrl+d(或 Ctrl+z 手动生成 EOF
打瞌睡,但请参阅:CTRL+Z does not generate EOF in Windows 10);
.bad()
(badbit
) 已设置。发生不可恢复的流错误;和
.fail()
(failbit
) 已设置。发生了匹配或其他可恢复的故障。发生故障时 输入停止并且不再从输入缓冲区中提取字符 .
(您可以将 1 和 2 合并为一个测试,因为此时输入已结束)
使用failbit
,你必须做两件事。 (1) 您必须通过调用 cin.clear()
清除流错误状态,并且 (2) 您必须处理输入缓冲区中任何未读的字符。通常这是通过包含 <limits>
和调用来处理的:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
(将从输入缓冲区中清除最多 INT_MAX
个字符,在遇到分隔符(此处为 '\n'
时停止)
一个循环直到用户输入有效 整数 的简短示例可以是:
#include <iostream>
#include <limits>
using namespace std;
int main (void) {
int x = 0;
while (1) /* loop continually until valid input received */
{
cout << "\nenter an integer: ";
if (! (cin >> x) ) { /* check stream state */
/* if eof() or bad() break read loop */
if (cin.eof() || cin.bad()) {
cerr << "(user canceled or unreconverable error)\n";
return 1;
}
else if (cin.fail()) { /* if failbit */
cerr << "error: invalid input.\n";
cin.clear(); /* clear failbit */
/* extract any characters that remain unread */
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
else { /* on succesful read of int */
/* extract any characters that remain unread */
cin.ignore(numeric_limits<streamsize>::max(), '\n');
break; /* then break read loop */
}
}
cout << "integer: " << x << '\n';
}
(如开头所述,只要涵盖所有三个条件,您可以采用多种不同的方式构建测试。)
此外,您可以通过调用 rdstate()
而不是使用 .fail()
等进行测试来明确检查流位...,例如
if (std::cin.rdstate() == std::ios_base::failbit)
例子Use/Output
$ ./bin/cin_validate_int
enter an integer: no
error: invalid input.
enter an integer: "an integer"
error: invalid input.
enter an integer: abc 123
error: invalid input.
enter an integer: 123
integer: 123
检查一下,如果您还有其他问题,请告诉我。
我正在制作一个乘法练习程序。正如我的标题所说,如果我在控制台中输入一个字母而不是一个数字,它会 运行 关闭说第一个正确,但其余的不正确。即使你没有触摸键盘,它仍然会吐出错误。
ans = table * i;
std::cout << table << " * " << i << " =" << std::endl;
std::cin >> input;
if(input == ans)
{
std::cout << "Correct! " << ans << std::endl;
}
else
{
std::cout << "Incorrect, the answer was " << ans << std::endl;
input = 0;
ans = 0;
}
}
希望这能让您了解代码中发生的事情。这是输出。
Enter the table you'd like to learn.
5
Enter the amount of multiples
3
5 * 0 =
s
Correct! 0
5 * 1 =
Incorrect, the answer was 5
5 * 2 =
Incorrect, the answer was 10
5 * 3 =
Incorrect, the answer was 15
我该怎么做才能解决这个问题?感谢您的输入。
当您执行 cin >> input
并且它无法将输入解析为数字时,它会 sets cin
's failbit
,并且当设置 failbit
时它实际上不再读取任何数据直到你清除它。
您应该通过调用 cin.good()
, and try to clear them with cin.clear()
, but you shouldn't try to clear some errors (like EOF) 检查错误,而不是应该在错误发生时退出。
有多种方法可以构建各种测试,但是当使用 cin
(或通常使用任何输入函数)进行输入时,您必须考虑输入缓冲区中未读的所有字符。使用 cin
,您必须验证三个条件:
.eof()
(eofbit
) 已设置。输入结束,或者用户通过按 Ctrl+d(或 Ctrl+z 手动生成EOF
打瞌睡,但请参阅:CTRL+Z does not generate EOF in Windows 10);.bad()
(badbit
) 已设置。发生不可恢复的流错误;和.fail()
(failbit
) 已设置。发生了匹配或其他可恢复的故障。发生故障时 输入停止并且不再从输入缓冲区中提取字符 .
(您可以将 1 和 2 合并为一个测试,因为此时输入已结束)
使用failbit
,你必须做两件事。 (1) 您必须通过调用 cin.clear()
清除流错误状态,并且 (2) 您必须处理输入缓冲区中任何未读的字符。通常这是通过包含 <limits>
和调用来处理的:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
(将从输入缓冲区中清除最多 INT_MAX
个字符,在遇到分隔符(此处为 '\n'
时停止)
一个循环直到用户输入有效 整数 的简短示例可以是:
#include <iostream>
#include <limits>
using namespace std;
int main (void) {
int x = 0;
while (1) /* loop continually until valid input received */
{
cout << "\nenter an integer: ";
if (! (cin >> x) ) { /* check stream state */
/* if eof() or bad() break read loop */
if (cin.eof() || cin.bad()) {
cerr << "(user canceled or unreconverable error)\n";
return 1;
}
else if (cin.fail()) { /* if failbit */
cerr << "error: invalid input.\n";
cin.clear(); /* clear failbit */
/* extract any characters that remain unread */
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
else { /* on succesful read of int */
/* extract any characters that remain unread */
cin.ignore(numeric_limits<streamsize>::max(), '\n');
break; /* then break read loop */
}
}
cout << "integer: " << x << '\n';
}
(如开头所述,只要涵盖所有三个条件,您可以采用多种不同的方式构建测试。)
此外,您可以通过调用 rdstate()
而不是使用 .fail()
等进行测试来明确检查流位...,例如
if (std::cin.rdstate() == std::ios_base::failbit)
例子Use/Output
$ ./bin/cin_validate_int
enter an integer: no
error: invalid input.
enter an integer: "an integer"
error: invalid input.
enter an integer: abc 123
error: invalid input.
enter an integer: 123
integer: 123
检查一下,如果您还有其他问题,请告诉我。