2016-10-30 1 views
-2

이 글은 제가 처음 작성한 프로그램 중 하나입니다.이 포럼이 정확한지 잘 모르겠습니다. 이 질문을하지만 누군가가 내 문제를 해결할 수 있는지 궁금 해서요.간단한 계산기가 예상대로 작동하지 않습니다. - 초보자 용 도움말 (C)

계산기에서 questionnaire 옵션을 선택하면 첫 번째 질문이 올바르게 작동하고 프로그램이 끝납니다. 중첩 된 switch 문은 실행되지 않습니다.

int selectOperation,firstValue,secondValue,questInput,userScore = 0,nextQuestion = 0; 

이가 무슨 의미가 있습니다

int main() { 

    /** Declaring variables (multiple variables can be declared together providing they are the same data type). **/ 
    int selectOperation,firstValue,secondValue,questInput,userScore,nextQuestion; 

    printf("\n***Welcome back, sir!***\n"); 

    do { 
      printf("\n***Please select your desired operation by inputting the associated value***\n\n"); 
      printf(" [1]: Addition \n [2]: Subtraction \n [3]: Division \n [4]: Multiplication \n [5]: Questionnaire \n"); 

      /** Asks the user to input an integer and stores the value in the selectOperation variable. **/ 
      printf("\nEnter value: "); 
      scanf("%d", &selectOperation); 

      /** While the selectOperation variable is not equal to 1, 2, 3, 4 or 5 continue to run this loop. **/ 
      /** The loop will continue to run until the user selects an operation. **/ 

    } while((selectOperation != 1) && (selectOperation != 2) && (selectOperation != 3) && (selectOperation != 4) && (selectOperation != 5)); 

    if ((selectOperation == 1) || (selectOperation == 2) || (selectOperation == 3) || (selectOperation == 4)) { 

     /** Input first value **/ 
     printf("\nPlease enter the first value you would like to process:\n"); 
     printf("\nEnter value: "); 
     scanf("%d", &firstValue); 

     /** Input second value **/ 
     printf("\nPlease enter the second value you would like to process:\n"); 
     printf("\nEnter value: "); 
     scanf("%d", &secondValue); 
} 

    /** If the selectOperation variable is equal to the case value the code for that case will be ran, for 
     example if selectOperation's current value is 1 the text "You have selected addition" will print." **/ 

    switch(selectOperation) { 

      case 1: /** Operation: Addition **/ 
        printf("\nYou have selected addition! Your answer can be found below:\n\n"); 
        /** Adds the stored values of the variables firstValue and secondValue and outputs the workings/result. **/ 
        printf("%d + %d = %d\n",firstValue,secondValue,firstValue+secondValue); 
        break; 

      case 2: /** Operation: Subtraction **/ 
        printf("\nYou have selected subtraction! Your answer can be found below:\n\n"); 
        /** Subtracts the value of secondValue from firstValue and outputs the workings/result. **/ 
        printf("%d - %d = %d\n",firstValue,secondValue,firstValue-secondValue); 
        break; 

      case 3: /** Operation: Division **/ 
        printf("\nYou have selected division! Your answer can be found below:\n\n"); 
        /** Divides firstValue by secondValue and outputs the workings/result. **/ 
        printf("%d/%d = %d\n",firstValue,secondValue,firstValue/secondValue); 
        break; 


      case 4: /** Operation: Multiplication **/ 
        printf("\nYou have selected multiplication! Your answer can be found below:\n\n"); 
        /** Multiplies firstValue by secondValue and outputs the workings/result. **/ 
        printf("%d * %d = %d\n",firstValue,secondValue,firstValue*secondValue); 
        break; 



      case 5: /** Questionnaire **/ 
        printf("\nQuestion 1: What is 48 * 3?\n"); 
        printf("\nEnter value: "); 
        scanf("%d", &questInput); 
        if (questInput == 144) { 
         printf("That's correct!\n"); 
         userScore = userScore + 1; 
         nextQuestion = nextQuestion + 1; 
       } 
        else { 
         printf("That's incorrect!\n"); 
         nextQuestion = nextQuestion + 1; 
       } 

        while(nextQuestion > 0 && nextQuestion < 5) { 

         switch(nextQuestion) { 

           case 1: 
             printf("\nQuestion 2: What is 36/6?\n"); 
             printf("\nEnter value: "); 
             scanf("%d", &questInput); 
              if (questInput == 6) { 
               printf("That's correct!\n"); 
               userScore = userScore + 1; 
               nextQuestion = nextQuestion + 1; 
              } 
              else { 
               printf("That's incorrect!\n"); 
               nextQuestion = nextQuestion + 1; 
              } 

             break; 

           case 2: 
             printf("\nQuestion 3: What is 1526 + 49?\n"); 
             printf("\nEnter value: "); 
             scanf("%d", &questInput); 
              if (questInput == 1575) { 
               printf("That's correct!\n"); 
               userScore = userScore + 1; 
               nextQuestion = nextQuestion + 1; 
              } 
              else { 
               printf("That's incorrect!\n"); 
               nextQuestion = nextQuestion + 1; 
              } 

             break; 

           case 3: 
             printf("\nQuestion 4: What is 5 * (5 + 15)?\n"); 
             printf("\nEnter value: "); 
             scanf("%d", &questInput); 
              if (questInput == 100) { 
               printf("That's correct!\n"); 
               userScore = userScore + 1; 
               nextQuestion = nextQuestion + 1; 
              } 
              else { 
               printf("That's incorrect!\n"); 
               nextQuestion = nextQuestion + 1; 
              } 

              break; 

           case 4: 
             printf("\nQuestion 5: What is 30 + 21?\n"); 
             printf("\nEnter value: "); 
             scanf("%d", &questInput); 
              if (questInput == 51) { 
               printf("That's correct!\n"); 
               userScore = userScore + 1; 
               nextQuestion = nextQuestion + 1; 
              } 
              else { 
               printf("That's incorrect!\n"); 
               nextQuestion = nextQuestion + 1; 
              } 

              break; 
     } 

     } 

    } 
} 
+0

코드 서식을 다시 지정하십시오. – Hugal31

+0

Welcome to Stack Overflow! 지금까지 연구/디버깅 노력을 보여주십시오. 먼저 [Ask] 페이지를 읽으십시오. –

+0

초기 입력을 제외하고 * 루프가 * 없기 때문입니다. 이것은 a) 적절하게 들여 쓰기 된 코드와 b) 더 작은 함수로 나누어 진 코드로보기가 더 쉬울 것입니다. BTW는 inintialised 변수 인'userScore'와'nextQuestion'을 사용하고 있습니다. –

답변

0

변경

int selectOperation,firstValue,secondValue,questInput,userScore,nextQuestion; 

옵션 1 ~ 4 번 다음 프로그램이 종료를 실행합니다. 옵션 5는 일련의 질문을 한 다음 프로그램을 종료합니다.

+0

네, 맞습니다. 당신이 제안한 것처럼 이미 변수를 변경했지만 질문 2, 3, 4 및 5는 여전히 실행되지 않습니다 (while 루프 내부). 이것에 대한 이유는 무엇입니까? – Gaarsin

+0

죄송합니다. 무슨 뜻인지 이해가 안됩니다. 옵션 1부터 5까지는 루프 중에는 없습니다. 나는 프로그램을 돌리고 있는데, 1에서 4까지의 옵션은 하나의 산술 연산자에 대한 입력을 요구한다. 옵션 5는 일련의 질문을합니다. 각각의 5 가지 옵션이 끝나면 프로그램이 종료됩니다. 하지만 한 가지 조언 : * 제수가 0 인 경우 옵션 4의 0 오류로 나누기. –

+0

... 나는 옵션 3, 부서를 의미합니다. –

관련 문제