c++ if语句导致无限循环
c++ if statement is causing an infinite loop
在我的函数中,我添加了一个if语句来保持用户输入正确的坐标,如果第一个if语句为假,它将导致无限循环。如果第一个IF语句是正确的,第二个IF语句是假的,它就可以正常工作。
void Sheep::sheepProcess(BoardSet& board)
{
char middle;
cout << "Your move? ";
cin >> x1 >> y1 >> middle >> x2 >> y2;
if (correctInput(x1, y1, x2, y2))
{
convertCoordinates(x1, y1, x2, y2); //x=z and y=w
if(board.legalMoveForSheep(z1, w1, z2, w2))
{
board.adjustBoardForSheep(z1, w1, z2, w2);
}
else
{
cout << "Illegal Move, Try again" << endl;
sheepProcess(board);
}
}
else
{
cout << "Illegal input, Try again" << endl;
sheepProcess(board);
}
}
如果您需要更多的代码,如bools。让我知道
编辑:class Sheep {
public:
void sheepProcess(BoardSet& board);
void convertCoordinates(char x1, int y1, char x2, int y2);
void initalizedItems();
private:
char x1, x2;
int y1, y2, z1, z2, w1, w2;
};
您不是在吃cin
上准备好的输入。下一次读取得到的是同样的输入,同样失败,不断重复。
解决方案:当你发现现成的输入无效时,吃掉它&扔掉,向用户抱怨提供了错误的输入,并允许他重试。
相关文章:
- 过载'operator new'如何导致无限循环?
- C++正则表达式无限循环
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 遍历链表时的无限循环
- 循环链表:无限循环
- 比较两个字符串后卡在无限循环中
- 在做一段时间内检查字符的无限循环
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 可能的无限循环
- 当我的 if 语句在字符串中找到空格时,在无限循环中切换
- 对程序使用 switch 语句,导致选项的无限循环
- 开关大小写语句中函数中的字符串输入会导致无限循环和绕过输入
- 如何修复当 switch 语句处于 while 循环中时不断发生的无限循环
- Switch 语句在使用默认案例 c++ 时陷入无限循环
- do 循环语句导致无限循环
- 数独求解矩阵,而语句给出了一个无限循环
- 为什么我在do while语句中得到了无限循环
- while循环中无限循环的switch语句
- c++ if语句导致无限循环