英文:
Cases in nested switch case won't run
问题
这个程序是用来计算菜品价格的。用户需要输入项目编号,它将根据用户选择的大小计算菜品的总价格。但是,第二个switch case(在switch(item_number)内部的那个)没有按预期工作。它只适用于case 1,其他case 2-4不起作用,因此程序无法显示准确的价格。我绝对不能使用if...else来完成此任务,必须使用switch case。
#include <stdio.h>
int main()
{
int item_number, dish_quantity;
char dish_size;
float price, total;
//显示菜单
printf("\t\t\t\tMenu\t\t\t\t\n");
printf("--------------------------------------------------------------------\n");
printf("Item Number Dish\t\t\t Dish Size \n");
printf("\t\t\t\tSmall(RM) Medium(RM) Large(RM)\n");
printf("--------------------------------------------------------------------\n");
printf(" 1 Ikan Tenggiri 10.00 20.00 30.00\n");
printf(" 2 Ikan Pari 8.00 15.00 25.00\n");
printf(" 3 Ikan Jenahak 9.00 17.00 28.00\n");
printf(" 4 Daging 10.00 20.00 30.00\n");
printf("--------------------------------------------------------------------\n\n");
// 用户输入
printf("Enter item number : ");
scanf("%d", &item_number);
printf("Enter dish size (S/M/L) : ");
scanf(" %c", &dish_size);
printf("Enter dish quantity : ");
scanf("%d", &dish_quantity);
switch (item_number){
case 1: switch (dish_size){
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;}
case 2: switch (dish_size){
case 's': case 'S': price = 8.00; break;
case 'm': case 'M': price = 15.00; break;
case 'l': case 'L': price = 25.00; break;}
case 3: switch (dish_size){
case 's': case 'S': price = 9.00; break;
case 'm': case 'M': price = 17.00; break;
case 'l': case 'L': price = 28.00; break;}
case 4: switch (dish_size){
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;}
}
printf("Price per dish size : RM %.2f\n", price);
total = dish_quantity * price;
printf("Total charge : RM %.2f", total);
return 0;
}
英文:
This program is to calculate the price of dishes. The user needs to input the item number and it'll calculate the total price of the dish based on the size they chose. However, the 2nd switch case (the one inside the switch (item_number)) doesn't work as expected. It works only for case 1, the other cases 2-4 doesn't work, hence the program cannot display the accurate price. I absolutely cannot do this assignment using if...else, it has to be with switch case.
#include <stdio.h>
int main()
{
int item_number, dish_quantity;
char dish_size;
float price, total;
//Display the menu
printf("\t\t\t\tMenu\t\t\t\t\n");
printf("--------------------------------------------------------------------\n");
printf("Item Number Dish\t\t\t Dish Size \n");
printf("\t\t\t\tSmall(RM) Medium(RM) Large(RM)\n");
printf("--------------------------------------------------------------------\n");
printf(" 1 Ikan Tenggiri 10.00 20.00 30.00\n");
printf(" 2 Ikan Pari 8.00 15.00 25.00\n");
printf(" 3 Ikan Jenahak 9.00 17.00 28.00\n");
printf(" 4 Daging 10.00 20.00 30.00\n");
printf("--------------------------------------------------------------------\n\n");
// Input from user
printf("Enter item number : ");
scanf("%d", &item_number);
printf("Enter dish size (S/M/L) : ");
scanf("%s", &dish_size);
printf("Enter dish quantity : ");
scanf("%d", &dish_quantity);
switch (item_number){
case 1: switch (dish_size){
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;}
case 2: switch (dish_size){
case 's': case 'S': price = 8.00; break;
case 'm': case 'M': price = 15.00; break;
case 'l': case 'L': price = 25.00; break;}
case 3: switch (dish_size){
case 's': case 'S': price = 9.00; break;
case 'm': case 'M': price = 17.00; break;
case 'l': case 'L': price = 28.00; break;}
case 4: switch (dish_size){
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;}
}
printf("Price per dish size : RM %.2f\n", price);
total = dish_quantity * price;
printf("Total charge : RM %.2f", total);
return 0;
}
答案1
得分: 4
The break
语句只会跳出最内层的switch
,而不会跳出外层的switch
。因此,您还需要为外层的switch
语句添加break
语句:
switch (item_number)
{
case 1:
switch (dish_size)
{
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;
}
break;
case 2:
switch (dish_size)
{
case 's': case 'S': price = 8.00; break;
case 'm': case 'M': price = 15.00; break;
case 'l': case 'L': price = 25.00; break;
}
break;
case 3:
switch (dish_size)
{
case 's': case 'S': price = 9.00; break;
case 'm': case 'M': price = 17.00; break;
case 'l': case 'L': price = 28.00; break;
}
break;
case 4:
switch (dish_size)
{
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;
}
break;
}
另一个问题是这一行:
scanf("%s", &dish_size);
会导致缓冲区溢出,因为&dish_size
只有足够的空间来存储一个字符,包括终止的空字符。因此,只有足够的空间来存储一个空字符串。
因此,我建议您将该行更改为以下内容:
scanf(" %c", &dish_size);
这样,scanf
将尝试写入一个单个字符,而不是以空字符结尾的字符串。
英文:
The break
statement will only break out of the innermost switch
, but not the outer switch
. Therefore, you also need break
statements for the outer switch
statement:
switch (item_number)
{
case 1:
switch (dish_size)
{
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;
}
break;
case 2:
switch (dish_size)
{
case 's': case 'S': price = 8.00; break;
case 'm': case 'M': price = 15.00; break;
case 'l': case 'L': price = 25.00; break;
}
break;
case 3:
switch (dish_size)
{
case 's': case 'S': price = 9.00; break;
case 'm': case 'M': price = 17.00; break;
case 'l': case 'L': price = 28.00; break;
}
break;
case 4:
switch (dish_size)
{
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;
}
break;
}
Another problem is that the line
scanf("%s", &dish_size);
will cause a buffer overflow, because &dish_size
only has room for storing a single character, including the terminating null character. Therefore, there is only sufficient room for storing an empty string.
For this reason, I suggest that you change that line to the following:
scanf(" %c", &dish_size);
That way, scanf
will only attempt to write a single character instead of a null-terminated string.
答案2
得分: 2
You are missing the break; statements in the outer case blocks.
switch (item_number){
case 1: switch (dish_size){
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;}
break; // add this at the end of each case.
英文:
You are missing the break; statements in the outer case blocks.
switch (item_number){
case 1: switch (dish_size){
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;}
break; // add this at the end of each case.
答案3
得分: 2
You need a break statement:
case 1: switch (dish_size){
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;
} break; //here for every case
And it's always a good idea to include a default
case for every switch
.
英文:
You need a break statement:
case 1: switch (dish_size){
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;
} break; //here for every case
And it's always a good idea to include a default
case for every switch
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论