【问题标题】:Why does my program loop the output infinitely when the user inputs anything besides and integer? C++ [duplicate]当用户输入除整数以外的任何内容时,为什么我的程序会无限循环输出? C++ [重复]
【发布时间】:2017-02-05 22:08:52
【问题描述】:

因此,用户应该能够输入 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;
}

【问题讨论】:

  • 那是因为你将一个字符分配给一个循环内的整数。
  • 对不起,我没有正确用词。它应该循环,但它只是无限循环提示的第一行,而不是让用户输入其他内容,就像整数超出范围一样。

标签: c++ arrays loops 2d infinite


【解决方案1】:

您没有处理甚至检查读取失败。代码

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;
    }
}

此外,您可能应该将文件结尾作为单独的案例处理。以防万一您的程序被从文件重定向的输入调用。

【讨论】:

  • 我认为std::cin 没有.reset(),您的意思是.clear()
  • 是的,刚刚修好了
  • 这段代码给了我在streamsize之后的">"操作数的错误,因为numeric_limits不是标准的。
  • @Jason numeric_limitslimits 标头中定义。
【解决方案2】:

您的代码似乎有错误。比如这个语句:

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,您将处于无限循环中。所以,我再次认为你的算法有问题。

【讨论】:

    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多