为什么当用户输入除整数之外的任何内容时,我的程序会无限循环输出?C++
Why does my program loop the output infinitely when the user inputs anything besides and integer? C++
因此,用户应该能够输入1-15之间的数字来移动2D游戏板上的瓷砖。 如果用户输入任何不在范围内的整数,程序将输出输入其他内容的提示。 但是,如果他们输入一个字符,由于某种原因,程序只是无限循环,而不是中继相同的提示。
这是main((中的代码
while(!found) //if the input is not found on the board, or the user selected an illegal tile, this retry prompt will appear until a legal tile is selected
{
cout << "This tile is not on the board or is not adjacent to the blank tile." << 'n' << "Please enter another numerical value between 1 and 16: ";
cin >> movet;
cout << endl;
found = moveTile(board, movet, blanki, blankj);
}
下面是返回 found 值的函数:
bool moveTile(int gameBoard[][SIZE], int nextMove, int &blanki, int &blankj)
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(gameBoard[i][j] == nextMove)
{
while(i == blanki - 1 || i == blanki + 1 || j == blankj + 1 || j == blankj - 1)//ensures the selected tile is within the surrounding 8 tiles
{
if( (i == blanki + 1 && j == blankj + 1)||(i == blanki - 1 && j == blankj - 1) || (i == blanki -1 && j == blankj + 1) || ( i == blanki + 1 && j == blankj - 1) )//removes corner tiles from possible selection to prevent illegal movement of game piece
{
return false; //if the selected value is a corner piece, the program will prompt the user to select something else
}
int temp = gameBoard[i][j];//saves original position into temp
gameBoard[i][j] = gameBoard[blanki][blankj];//stores moved tile into blank tile position
gameBoard[blanki][blankj] = temp;//stores the blank tile into the moved tile's position
blanki = i;//keeps track of the blank tile's position
blankj = j;
return true;
}
}
}
}
return false;
}
您没有处理甚至检查读取失败。代码
cin >> movet;
尝试读取整数。如果输入的不是整数,则读取失败,失败状态设置为 cin
。一旦设置了cin
的故障位,所有后续的读取操作都将失败。您可以测试cin
以查看读取是否成功,也可以使用 cin.clear()
重置流状态。例如:
while (!found) {
std::cout << "This tile is not on the board or is not adjacent to "
<< "the blank tile.nPlease enter another numerical value "
<< "between 1 and 16: ";
if (std::cin >> movet)
found = moveTile(board, movet, blanki, blankj);
else { //invalid input, reset stream and try again
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
found = false;
}
}
此外,您可能应该将文件结尾作为单独的案例处理。以防万一调用程序时输入从文件重定向。
您的代码中似乎有错误。例如此语句:
while(i==blanki-1||i==blanki+1||j==blankj+1||j==blankj-1)
像这样的巴赫维斯:
if(i==blanki-1||i==blanki+1||j==blankj+1||j==blankj-1)
因为您在第一次迭代中返回。因此,首先看看您的算法或在您的问题中解释它。当 moveTile 返回 False 时,您不会更改任何 int gameBoard,blanki 或 blankj。因此,当此函数返回 False 时,它将返回 false,如果您再次调用它,您将处于无限循环中。所以,我再次认为你的算法存在问题。
相关文章:
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的 IExtractIcon 处理程序没有被调用?
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 我的程序没有从文件中读取并输入数据
- 我的评分程序无法正常工作
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 反向功能超出了我的 cpp 程序的范围
- 我的二进制搜索程序只是关闭了
- 为什么我的程序不能显示斐波那契级数?
- 为什么我的程序在使用预留后没有加速?
- 为什么如果我添加这一行,我的程序会不断询问值
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 为什么我的两个 cin 语句没有在程序结束时运行?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- C++机器人程序(我的私人服务器)出现问题
- 用C++创建一个写作应用程序(我的第一个项目)(学习C++)