2017-10-28 7 views
0

whileループ実行後にコードがシャットダウンし、最後の2つのprintf文が実行されません。私は何が間違っているのかわからない。ループが選択された時間の周りを回った後、プログラムはちょうど終了する。whileループ実行後にプログラムがクラッシュする

#include <stdio.h> 
int main() 
{ 
    int numberofq; 
    int questionansc; 
    int questionansic; 
    int counter; 
    int answer; 

    numberofq = 0; 
    questionansc = 0; 
    questionansic = 0; 
    counter = 0; 
    answer = 0; 

    while(numberofq <1 || numberofq >5) 
    { 
    printf("Hello enter the amount of questions you want between 1 and 5 \n"); 
    scanf("%d", &numberofq); 
    } // End While 

    //Program runs until the counter is less than users wanted question number. 
    while (counter < numberofq) 
    { 
     //Question 1 
     printf("Question 1. what is 2+2? \n"); 
     scanf("%d" , &answer); 

     //if users answer is equal to 4. 
     if (answer == 4) 
     { 
     printf("You entered %d, you are correct\n", answer); 
     questionansc = questionansc +1; 
     } //End If 

     //If the answer is not equal to 4. 
     else 
     { 
     printf("You entered %d, you are wrong correct answer is 4 \n", answer); 
     questionansic = questionansic +1; 
     } // End Else 

     counter = counter +1; 
     //End Question 1. 

    } //End While 

     printf("You got %d questions correct \n" , questionansc); 
     printf("You got %d questions wrong" , questionansic); 
     flushall(); 

     return 0; 
} // End Main` 
+0

使用している言語をタグに追加します。 – Neo

+0

ヒント - あなたが宣言した同じ行の変数を初期化(あなたの場合は0に設定)することができます。 – Neo

答えて

1

は、それは実際にそれらと、その後終了を出力しますが、それはすぐにあなたがこれを見る機会を持っていないので終了します。
Windows上でsystem("pause")を使用して実行を一時停止することはできますが、それは悪い方法です。 getch()などを使うこともできますが、単に既存のCMD/Terminalからプログラムを呼び出すこともできます。このようにして、プログラムの実行後も出力がそのまま残ります。

関連する問題