嵌套的 switch case 中的情况不会运行。

huangapple go评论63阅读模式
英文:

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 &lt;stdio.h&gt;
int main()
{
int item_number, dish_quantity;
char dish_size;
float price, total;
//Display the menu
printf(&quot;\t\t\t\tMenu\t\t\t\t\n&quot;);
printf(&quot;--------------------------------------------------------------------\n&quot;);
printf(&quot;Item Number   Dish\t\t\t Dish Size              \n&quot;);
printf(&quot;\t\t\t\tSmall(RM)   Medium(RM)   Large(RM)\n&quot;);
printf(&quot;--------------------------------------------------------------------\n&quot;);
printf(&quot;     1        Ikan Tenggiri       10.00      20.00        30.00\n&quot;);
printf(&quot;     2        Ikan Pari            8.00      15.00        25.00\n&quot;);
printf(&quot;     3        Ikan Jenahak         9.00      17.00        28.00\n&quot;);
printf(&quot;     4        Daging              10.00      20.00        30.00\n&quot;);
printf(&quot;--------------------------------------------------------------------\n\n&quot;);
// Input from user
printf(&quot;Enter item number        : &quot;);
scanf(&quot;%d&quot;, &amp;item_number);
printf(&quot;Enter dish size (S/M/L)  : &quot;);
scanf(&quot;%s&quot;, &amp;dish_size);
printf(&quot;Enter dish quantity      : &quot;);
scanf(&quot;%d&quot;, &amp;dish_quantity);
switch (item_number){
case 1: switch (dish_size){
case &#39;s&#39;: case &#39;S&#39;: price = 10.00; break;
case &#39;m&#39;: case &#39;M&#39;: price = 20.00; break;
case &#39;l&#39;: case &#39;L&#39;: price = 30.00; break;}
case 2: switch (dish_size){
case &#39;s&#39;: case &#39;S&#39;: price = 8.00; break;
case &#39;m&#39;: case &#39;M&#39;: price = 15.00; break;
case &#39;l&#39;: case &#39;L&#39;: price = 25.00; break;}
case 3: switch (dish_size){
case &#39;s&#39;: case &#39;S&#39;: price = 9.00; break;
case &#39;m&#39;: case &#39;M&#39;: price = 17.00; break;
case &#39;l&#39;: case &#39;L&#39;: price = 28.00; break;}
case 4: switch (dish_size){
case &#39;s&#39;: case &#39;S&#39;: price = 10.00; break;
case &#39;m&#39;: case &#39;M&#39;: price = 20.00; break;
case &#39;l&#39;: case &#39;L&#39;: price = 30.00; break;}
}
printf(&quot;Price per dish size      : RM %.2f\n&quot;, price);
total = dish_quantity * price;
printf(&quot;Total charge             : RM %.2f&quot;, 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 &#39;s&#39;: case &#39;S&#39;: price = 10.00; break;
case &#39;m&#39;: case &#39;M&#39;: price = 20.00; break;
case &#39;l&#39;: case &#39;L&#39;: price = 30.00; break;
}
break;
case 2:
switch (dish_size)
{
case &#39;s&#39;: case &#39;S&#39;: price = 8.00; break;
case &#39;m&#39;: case &#39;M&#39;: price = 15.00; break;
case &#39;l&#39;: case &#39;L&#39;: price = 25.00; break;
}
break;
case 3:
switch (dish_size)
{
case &#39;s&#39;: case &#39;S&#39;: price = 9.00; break;
case &#39;m&#39;: case &#39;M&#39;: price = 17.00; break;
case &#39;l&#39;: case &#39;L&#39;: price = 28.00; break;
}
break;
case 4:
switch (dish_size)
{
case &#39;s&#39;: case &#39;S&#39;: price = 10.00; break;
case &#39;m&#39;: case &#39;M&#39;: price = 20.00; break;
case &#39;l&#39;: case &#39;L&#39;: price = 30.00; break;
}
break;
}

Another problem is that the line

scanf(&quot;%s&quot;, &amp;dish_size);

will cause a buffer overflow, because &amp;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(&quot; %c&quot;, &amp;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 &#39;s&#39;: case &#39;S&#39;: price = 10.00; break;
case &#39;m&#39;: case &#39;M&#39;: price = 20.00; break;
case &#39;l&#39;: case &#39;L&#39;: 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 &#39;s&#39;: case &#39;S&#39;: price = 10.00; break;
case &#39;m&#39;: case &#39;M&#39;: price = 20.00; break;
case &#39;l&#39;: case &#39;L&#39;: price = 30.00; break;
} break;                                      //here for every case

And it's always a good idea to include a default case for every switch.

huangapple
  • 本文由 发表于 2023年5月10日 18:44:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定