2016-04-17 5 views
0

Я пытаюсь использовать оператор switch для выбора между треугольной призмой или треугольной пирамидой. Когда я выбираю 1, он говорит мне, что значение должно быть между 1 и 2. Когда я выбираю 2, он выбирает треугольную пирамиду и, похоже, работает.Switch switch breaks menu

#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

Используйте '% d' для' int' – Idos

+0

В 'if (menu1 == 2)' для каждого случая существует 'break'', но если это условие не выполняется, разрыв никогда не произойдет. – Unimportant

ответ

1

Вы должны быть очень осторожны: коды форматирования в scanf() должен соответствовать типу адреса вы предоставляете в качестве дополнительного аргумента:

  • "%f" нормально для поплавков как OPT1, ОРТ2 и т.д. ..
  • Но для междунар вам нужно "%d"
  • Больше комбинаций here

Если есть несоответствие, вы не только получите правильное значение в вашей переменной (потому что кодирование с плавающей запятой 1 не будет таким же, как целое число 1), но вы рискуете также переполнением буфера и повреждением памяти (если sizeof(float) больше, чем sizeof(int)).