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);
}
}
}
'%f 'とは何ですか? –
'int 'に'%d'を使用する – Idos
各ケースの 'if(menu1 == 2)'の中に 'break'がありますが、この条件が満たされなければブレークは起こりません。 – Unimportant