2016-04-17 11 views
0

switch文を使用して三角柱または三角ピラミッドを選択しようとしています。私が1を選択すると、値が1と2の間でなければならないことがわかります。2を選択すると、三角ピラミッドが選択され、動作するように見えます。switch文のブレークメニュー

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    float tt, menu1, opt1, opt2, opt3, t, opt4; 
    int td; 
    printf("Enter: "); 
    scanf("%f",&tt); 
{ 
       printf("\nWhat geometrical figure would you like to use for Volume?\n\n"); 
       printf("1) Triangular Prism\n"); 
       printf("2) Triangular Pyramid\n"); 
       printf("User choice: "); 
       scanf("%f", &td); 
       while (td < 1 || td > 2) { 
        printf("\nUser choice must be between 1 and 2!\n\n"); 
        printf("\nWhat geometrical figure would you like to use for Volume?\n\n"); 
        printf("1) Triangular Prism\n"); 
        printf("2) Triangular Pyramid\n"); 
        printf("User choice: "); 
        scanf("%d", &td); 
       } 
       switch(td) { 
        case 1: 
        printf("Enter a, b, c, and h of the triangular prism in meters\n\n"); 
        printf("a "); 
        scanf("%f", &opt1); 
        printf("b "); 
        scanf("%f", &opt2); 
        printf("c "); 
        scanf("%f", &opt3); 
        printf("h "); 
        scanf("%f", &opt4); 
        printf("\nWould you like to make another Volume calculation (1 for Yes, 2 for No)?"); 
        scanf("%f", &menu1); 
        if (menu1 == 2) { 
         t = 0; 
         break; 
        } 
        if (menu1 < 1 || menu1 > 2) { 
         printf("\n\nUser choice must be between 1 and 2!\n\n"); 
         printf("Would you like to make another Volume calculation (1 for Yes, 2 for No)?"); 
         scanf("%f", &menu1); 
        } 
       case 2: 
        printf("Enter a and h of the triangular pyramid\n\n"); 
        printf("a "); 
        scanf("%f", &opt1); 
        printf("h "); 
        scanf("%f", &opt2); 
        printf("\nWould you like to make another Volume calculation (1 for Yes, 2 for No)?"); 
        scanf("%f", &menu1); 
        if (menu1 == 2) { 
         t = 0; 
         break; 
        } 
        if (menu1 < 1 || menu1 > 2) { 
         printf("\n\nUser choice must be between 1 and 2!\n\n"); 
         printf("Would you like to make another Volume calculation (1 for Yes, 2 for No)?"); 
         scanf("%f", &menu1); 
        } 

      } 
} 
      if (tt == 4) { 
       printf("Enter the radius of the circle\n\n"); 
       printf("Radius: "); 
       scanf("%f", &opt1); 
       printf("\nWould you like to make another Volume calculation (1 for Yes, 2 for No)?"); 
       scanf("%f", &menu1); 
       if (menu1 == 2) { 
        t = 0; 
       } 
       if (menu1 < 1 || menu1 > 2) { 
        printf("\n\nUser choice must be between 1 and 2!\n\n"); 
        printf("Would you like to make another Volume calculation (1 for Yes, 2 for No)?"); 
        scanf("%f", &menu1); 
       } 
      } 
} 
+1

'%f 'とは何ですか? –

+0

'int 'に'%d'を使用する – Idos

+0

各ケースの 'if(menu1 == 2)'の中に 'break'がありますが、この条件が満たされなければブレークは起こりません。 – Unimportant

答えて

1

あなたは非常に注意する必要があります:

  • "%f"などOPT1、OPT2、などの山車のためokです:scanf()でフォーマッティングコードはあなたに追加の引数を与えたアドレスのタイプと一致する必要があります...
  • しかし、int型のために必要な"%d"
  • 以上の組み合わせhere

不一致がある場合は、変数の正しい値を取得できないだけでなく(1の浮動小数点のエンコーディングは整数1と同じではないため)、バッファオーバーフローやメモリ破損のリスクもありますsizeof(float)sizeof(int)より大きい)。