2017-10-28 1 views
0

while 루프를 실행 한 후 코드가 종료되고 마지막 두 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/터미널에서 프로그램을 호출하기 만하면이 방법으로 프로그램을 완료 한 후에 출력이 그대로 유지됩니다.