2017-02-05 4 views
-2

したがって、ユーザーは2Dボード上のタイルを移動するために1〜15の間の数字を入力できるはずです。ユーザーが範囲にない整数を入力すると、プログラムは何か他のものを入力するようにプロンプ​​トを出力します。しかし、文字を入力すると、何らかの理由で同じプロンプトを中継するのではなく、無限にループします。ユーザが整数と整数を入力したときに、私のプログラムが出力を無限ループするのはなぜですか? C++

ここでは、メインのコードが(ある)

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

そして、ここでは、見つかったの値を返す関数です:

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

これは、ループ内の整数に文字を割り当てるためです。 – Raindrop7

+0

申し訳ありませんが正しく言わなかった。それはループする必要がありますが、ユーザーが範囲外の整数を持つような何かを入力させる代わりに、プロンプトの最初の行を無限にループするだけです。 – Jason

答えて

0

あなたは、取り扱い、あるいは読み取りの失敗をチェックされていません。コード

cin >> movet; 

整数を読み込もうとします。整数以外の値が入力された場合、読み取りは失敗し、失敗状態はcinに設定されます。 cinのfailbitが設定されると、その後のすべての読み取り操作は失敗します。 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; 
    } 
} 

また、ファイルの終わりを別のケースとして処理する必要があります。ちょうどあなたのプログラムがファイルからリダイレクトされた入力で呼び出された場合です。

+1

'std :: cin'は' .reset() 'を持っていないと思いますが、' .clear() 'を意味しましたか? – Winestone

+0

いいえ、ちょうどそれを修正しました –

+0

このコードは、streamsizeの後に ">"オペランドでエラーが発生します。numeric_limitsは標準ではないためです。 – Jason

0

あなたのコードに間違いがあるようです。たとえば、次のステートメント:このような

while(i==blanki-1||i==blanki+1||j==blankj+1||j==blankj-1) 

bahves:あなたが最初の反復に戻るBecuase

if(i==blanki-1||i==blanki+1||j==blankj+1||j==blankj-1) 

。だから、まず、あなたのアルゴリズムを見てみたり、あなたの質問にそのことを説明してください。 moveTileがFalseを返した場合、gameBoard、blankiまたはblankjのいずれも変更しません。したがって、この関数がFalseを返すと、それを再度呼び出すとfalseが返され、無限ループになります。だから、私はあなたのアルゴリズムに問題があると思います。

関連する問題