为什么当用户输入除整数之外的任何内容时,我的程序会无限循环输出?C++

Why does my program loop the output infinitely when the user inputs anything besides and integer? C++

本文关键字:程序 我的 无限循环 C++ 输出 任何内 输入 用户 整数 为什么      更新时间:2023-10-16

因此,用户应该能够输入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,如果您再次调用它,您将处于无限循环中。所以,我再次认为你的算法存在问题。