2016-10-30 3 views
-2

これは私がこれまでに書いた最初のプログラムですが、これはとても新しいものです。これを尋ねるのに誰かが私の問題を解決するのに役立つかどうか疑問に思っていました。シンプルな電卓が期待通りに動作しない - 初心者のヘルプ(C)

私の電卓からアンケートオプションを選択すると、最初の質問が正しく機能し、プログラムが終了します。ネストされた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

ようこそスタックオーバーフロー!これまでのところあなたの研究/デバッグの努力を示してください。まず[Ask]ページをお読みください。 –

+0

これは、最初の入力とは別に*ループがないためです。これは、a)適切に字下げされたコードと、b)小さな関数に分割されたコードで分かりやすくなります。あなたは2つの初期化されていない変数、 '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は、1つの算術演算子の入力を要求します。オプション5は、一連の質問をします。これらの5つのオプションのそれぞれの後、プログラムは終了します。しかし、私は1つのことをお勧めします:*除数が0である場合、オプション4の0で除算をトラップします。 –

+0

...私はオプション3、部門を意味します。 –

関連する問題