cin for an int inputing a char causes Loop that is supposed to check input to go wild(cin 用于输入 char 的 int 导致应该检查输入的循环变得疯狂)
问题描述
这是我的游戏的一个功能,它会要求输入和 cin 进入iAuswahl"!然后while循环检查它是否是我想要1-9的值之一,如果不是,它会激活并且应该要求新的输入.女巫它为int做.但是,如果我输入一个像 r 这样的字符,它会发疯,然后继续给我我的 cout 并跳过 cin!我的问题是它为什么会这样做,我该如何阻止它?
This is a function of my game it will ask for input and cin into "iAuswahl"! Then the while loop is checks if it is one of the Values i want 1-9 if not it activates and is supposed to ask for new input. Witch it does for int. But if i input a char like r it will go crazy and just keep giving me back my cout and skip the cin! My questions are why does it do it and how do i stop it?
void zug(string sSpieler, int iDran){
int iAuswahl;
char cXO = 'O';
if (iDran == 1)
{
cXO = 'X';
}
cout << sSpieler << ", Sie sind am Zug. Bitte waehlen sie eins der Felder.
" << endl;
grafik();
cout << "Sie sind >> " << cXO << " <<." << endl;
cin >> iAuswahl;
cout << endl;
while (
iAuswahl != 1
&& iAuswahl != 2
&& iAuswahl != 3
&& iAuswahl != 4
&& iAuswahl != 5
&& iAuswahl != 6
&& iAuswahl != 7
&& iAuswahl != 8
&& iAuswahl != 9
)
{
cout << "Kein gültiges Feld bitte wählen sie noch einmal!
" << endl;
cin >> iAuswahl;
}
feldfuellen(iAuswahl, cXO);
}
推荐答案
当从流中读取时发生错误时,会设置错误标志,并且在清除错误标志之前无法进行更多读取.这就是你得到一个无限循环的原因.
When an error occurs when reading from a stream, an error flag gets set and no more reading is possible until you clear the error flags. That's why you get an infinite loop.
cin.clear(); // clears the error flags
// this line discards all the input waiting in the stream
cin.ignore(std::numeric_limits<std::streamsize>::max(), '
');
另外,如果一开始不知道读取是否成功,使用输入操作的结果是错误的.您不能对 iAuswahl 的价值做出假设.这是新手使用流时最常犯的错误之一.经常检查输入操作是否正常.这很容易通过在布尔上下文中使用 operator>> 来完成:
Also, it's wrong to use the results of input operation if you don't know whether the read succeeded in the first place. You can't make assumptions about the value of iAuswahl. That's one of most often made errors by newbies using streams. Always check if the input operation was ok. This is most easily done by using operator>> in boolean context:
if (cin >> some_obj) {
// evaluates to true if it succeeded
} else {
// something went wrong
}
还有,天哪,这条线
while (iAuswahl != 1 && iAuswahl != 2 && iAuswahl != 3 && iAuswahl != 4 && iAuswahl != 5 && iAuswahl != 6 && iAuswahl != 7 && iAuswahl != 8 && iAuswahl != 9)
可以是这样的:
while (iAuswahl < 1 || iAuswahl > 9)
正确的循环可能如下所示:
A correct loop could look something like this:
while (true)
{
if ((cin >> iAuswahl) && (iAuswahl >= 1) && (iAuswahl <= 9)) break;
std::cout << "error, try again
";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '
');
}
这篇关于cin 用于输入 char 的 int 导致应该检查输入的循环变得疯狂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:cin 用于输入 char 的 int 导致应该检查输入的循环变得疯狂
基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
