Я пытаюсь использовать оператор 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);
}
}
}
Что означает '% f'? –
Используйте '% d' для' int' – Idos
В 'if (menu1 == 2)' для каждого случая существует 'break'', но если это условие не выполняется, разрыв никогда не произойдет. – Unimportant