2016-07-16 5 views
0

こんにちは、私はまだCを学んでいます。ここで私が思いついたのです。私の問題は、ユーザが変数 'number'を0以下に入力した場合、最初から始めることができないということです。また、私が尋ねる場合でも、関数を使用するときに最初にどのようにループするのでしょうか?Loop to beginning

void main() { 

    int x = 0, y = 0, m, n, number, life = 4; 
    char choice; 
    int a[R][C] = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} }; 

    do { 
     printf("\t\t\t  Welcome to my game"); 
     Sleep(2000); 
     printf("\n\n This game is all about guessing where your number is located in the matrix"); 
     Sleep(2000); 
     printf("\n\n\t\t\t\tOnly 1 player\n"); 
     Sleep(2000); 
     printf("\n\n You are betting your own very life! \t\t\tYou have 5 tries"); 
     Sleep(1000); 
     printf("\n\nEnter your number: "); 
     scanf("%d", &number); 
     system("cls"); 
     if (number <= 0) { 
      system("cls"); 
      printf("Only positive numbers allowed"); 
      printf("\nRetry? Y/N: "); 
      fflush(stdin); 
      scanf("%c", &choice); 
      fflush(stdin); 
      if (choice == 'n' || choice == 'N') { 
       system("cls"); 
       printf("Thanks for playing"); 
       break; 
      } 
      system("cls"); 
      break; 
     } 
     display(a); 
     printf("\n\nInputted number is %d", number); 
     printf("\n\nYour number is now being placed at a random location"); 
     printf("\n\nGuess where your number is located (row) (column)"); 
     printf("\n\nNote: Enter only numbers 1-4 or else you'll be wrong"); 
     printf("\n\nEnter coordinates: "); 
     scanf("%d%d", &m, &n); 
     printf("\n"); 
     placingguess(a, number, m, n, life); 
     break; 
    } while (choice != 'n' || choice != 'N'); 

    getch(); 
} 
+3

技術的には、 'fflush(stdin)'は定義された操作ではなく、コード内で未定義の動作をしています。いくつかの標準ライブラリでは拡張子が許されていますが、移植性のあるコードを書いておきたい場合は、習慣に入るべきではありません。 –

+1

プログラムを7000秒間スリープ状態にするのに関連性がありますか?さらに、 'stdin'の' fflush'は未定義の動作を引き起こします。 'cls'とは何ですか? – user3078414

+1

あなたの問題については、それらの 'break'ステートメントはループの中で何をドーピングしていますか?ループ内で何が起きているのか知っていますか? –

答えて

3

利用continueの代わりbreak

} 
    system("cls"); 
    continue; 
} 
display(a); 
+0

「続ける」のようなコマンドがあったことはまったく分かりませんでした。今それは働いている!ありがとう。 – Steven

2

あなたは

  • break
    break; 
    

    を使用していた -
  • continue ... while - - 次の反復fordo \ \現在whileを終了します。

第1のbreak;ユーザーがNを入力して再試行するので、 continueが必要です。

+0

ありがとう!今それは働いている。これまで「続行」というコマンドがあることは決して知りませんでした。 – Steven