尝试在循环语句内的开关中获取多个输入时出错

Error while trying to take multiple inputs in a switch inside a loop statement

所以我正在制作一个基本的 hotel/restaurant 计费系统,我正在打印一个菜单,然后在使用 switch 语句时将输入作为选项,问题是我可以添加一道菜 select 你想要那道菜的数量并加税并计算总账单,但我无法获得多个数量的多道菜的总账单。但是我能够得到价格相同但数量不同的多种菜品的总账单。


    #include<stdio.h>
    
    void main()
    {
        float food=0; // Total Food bill
        float p; // Quantity of food
        int ch; // Choice
        float vat,gst; // TAX
        float total; // Total bill
        int w; // Loop variable
    
    
        printf("\n ***Menu***\n Please choose items which you want to order from the list given below \n ---Tiffins--- \n 1. Plain Dosa = 50/- \n 2. Masala Dosa = 70/- \n 3. Onion Dosa = 80/- \n 4. Idli = 50/- \n 5. Uttapam = 90/- \n 6. Vada = 60/- \n 7. Upma = 65 /- \n 8. Pongal = 70/- \n 9. Puri = 70/- \n ");
        // Main Menu
        printf("\n Please enter your order: \n");
        scanf("%d",&ch); // Choice input
        do{ // Loop
            switch(ch)
        {
            case 1:
                printf("\n How many plates do you want to order: \n");
                scanf("%f",&p);
                food = food+50*p;
                printf("\nYour total as of right now: %.2f\n",food);
                break;
    
    
            case 2:
                printf("\n How many plates do you want to order: \n");
                scanf("%f",&p);
                food = food+70*p;
                printf("\nYour total as of right now: %.2f\n",food);
                break;
    
    
            case 3:
                printf("\n How many plates do you want to order: \n");
                scanf("%f",&p);
                food = food+80*p;
                printf("\nYour total as of right now: %.2f\n",food);
                break;
    
            case 4:
                printf("\n How many plates do you want to order: \n");
                scanf("%f",&p);
                food = food+50*p;
                printf("\nYour total as of right now: %.2f\n",food);
                break;
    
            case 5:
                printf("\n How many plates do you want to order: \n");
                scanf("%f",&p);
                food = food+90*p;
                printf("\nYour total as of right now: %.2f\n",food);
                break;
    
    
            case 6:
                printf("\n How many plates do you want to order: \n");
                scanf("%f",&p);
                food = food+60*p;
                printf("\nYour total as of right now: %.2f\n",food);
                break;
    
    
            case 7:
                printf("\n How many plates do you want to order: \n");
                scanf("%f",&p);
                food = food+65*p;
                printf("\nYour total as of right now: %.2f\n",food);
                break;
    
    
            case 8:
                printf("\n How many plates do you want to order: \n");
                scanf("%f",&p);
                food = food+70*p;
                printf("\nYour total as of right now: %.2f\n",food);
                break;
    
            case 9:
                printf("\n How many plates do you want to order: \n");
                scanf("%f",&p);
                food = food+70*p;
                printf("\nYour total as of right now: %.2f\n",food);
                break;
    
    
            default:
                printf("\nInvalid choice\n");
    
        }
    
        printf("\nPress 0 to go to billing\n");
        scanf("%d",&w);
    
    } while(w>0);
    
    vat = (7*food)/100;
    gst = (5*food)/100;
    total = food+vat+gst;
    printf("\n ---TOTAL BILL--- \n Food Total = %.2f \n Vat(7%%) = %.2f \n GST(5%%) = %.2f \n Total amount = %.2f \n",food,vat,gst,total);
    
    getch();
}

所以是的,这是一个愚蠢的基本初学者问题,但我们将不胜感激任何帮助。是的,我是印度人,是的,那些是印度菜。

谢谢!

编辑:好的,谢谢,现在它已修复,我所要做的就是将我的选择输入移动到循环中。谢谢大家的帮助!

在你的代码中,你写

printf("\nPress 0 to go to billing\n");
scanf("%d",&w);

}while(w>0);

但是,在检查中您检查的是大于 0 的值。您应该将其更改为

}while (w ==0);

就是说,如果您真的想要重复菜单,则需要将菜单的打印和用户输入移动到 do...while 循环中。

您是在循环外选择菜肴,因此您只要求一次。将执行此操作的代码移到顶部的循环内。这样它每次都会询问。

do{ // Loop
    printf("\n ***Menu***\n Please choose items which you want to order from the list given below \n ---Tiffins--- \n 1. Plain Dosa = 50/- \n 2. Masala Dosa = 70/- \n 3. Onion Dosa = 80/- \n 4. Idli = 50/- \n 5. Uttapam = 90/- \n 6. Vada = 60/- \n 7. Upma = 65 /- \n 8. Pongal = 70/- \n 9. Puri = 70/- \n "); // Main Menu
    printf("\n Please enter your order: \n");
    scanf("%d",&ch); // Choice input
    ...

问题是您在循环外读取菜品输入,这意味着在您 select 第一道菜之后,您将无法选择另一道菜:

printf("\n Please enter your order: \n");
scanf("%d",&ch); // Choice input
do{ // Loop
switch(ch)

如果你这样写:

do{ // Loop
printf("\n Please enter your order: \n");
scanf("%d",&ch); // Choice input
switch(ch)

用户将能够选择不同的菜肴。