MRJGAMER
MRJGAMER

Reputation: 23

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

So I am making a basic hotel/restaurant billing system and I am printing a menu and then taking inputs as choices while using a switch statement and the problem is that I am able to add one dish and select how many quantities of that dish you want and add tax and calculate total bill but I am unable to get a total bill for multiple dishes with multiple quantities. But I am able to get a total bill of multiple dishes having the same price and different quantities.


    #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();
}

So yes it is a stupid basic beginner question but any help will be greatly appreciated. And yes I am Indian and yes those are Indian dishes.

Thank you!

Edit: Ok thank you its is fixed now all I had to do was move my choice input inside the loop. Thank you all for your help!

Upvotes: 2

Views: 56

Answers (3)

Troloze
Troloze

Reputation: 23

The issue is that you read the dish input outside of the loop, that means that after you select the first dish, you won't be able to choose another one:

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

If you, instead, put your it like this:

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

The user will be able to choose diferent dishes.

Upvotes: 1

dbush
dbush

Reputation: 224387

You're selecting the dish outside of the loop, so you're only asking for it once. Move the code that does that to inside the loop at the top. That way it asks each time.

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
    ...

Upvotes: 2

Sourav Ghosh
Sourav Ghosh

Reputation: 134356

In your code, you write

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

}while(w>0);

however, in the check you're checking for a value greater than 0. You should change that to

}while (w ==0);

That said, if you really want the menu to be repeated, you need to move the print and user input for menu inside the do...while loop.

Upvotes: 0

Related Questions