Creating a Simple Menu Order System using Goto and Switch Statement in C


The above program is an extended version of the previous program which demonstrates the use of the goto statement along with a switch statement.

  • The program starts with the inclusion of the header file "stdio.h" which contains the functions used in the program, like printf() and scanf().
  • The program declares several integer variables: ch, qty, i, and net, which are used to represent the user's choice of product, the quantity of the product, a variable to check if the user wants to continue and a variable to keep track of the total amount respectively.
  • The program then uses the goto statement to jump to a labeled location in the program, which is "joes" in this case. The program then displays a menu card with 4 options: Coffee, Tea, Cold Coffee, and Milk Shake along with their prices. The user is prompted to enter their choice of product using the scanf() function.
  • The program then uses a switch statement to check the value of the user's choice and execute the corresponding code block. The switch statement compares the value of the variable 'ch' with the cases 1, 2, 3, and 4. If the value of 'ch' matches any of the cases, the corresponding code block is executed.
  • In each case, the program prompts the user to enter the quantity of the product and calculates the total amount of the order by multiplying the quantity with the price of the product and adding the result to the "net" variable.
  • After that, the program prompts the user to enter 1 if he wants to continue, if the user enters 1, the program goes back to the labeled location "joes" using the goto statement and the process continues. If the user enters any other number, the program does not jump back to the labeled location.
  • Finally, the program prints the total amount and "Thank You Come Again" and returns 0 to indicate successful execution.

As I previously mentioned, the use of goto statement is considered as a bad practice in programming and it makes the program harder to read and understand, and can lead to hard to debug spaghetti code. Instead of using goto statement, programmers should use structured control flow statements like while, do-while, for loop etc.


Source Code

#include<stdio.h>
 
int main()
{
    int ch,qty,i,net=0;
    joes:
    printf("\n\tMENU CARD");
    printf("\n\t\t1.COFFEE        Rs:15");
    printf("\n\t\t2.TEA           Rs:10");
    printf("\n\t\t3.COLD COFFEE   Rs:25");
    printf("\n\t\t4.MILK SHAKE    Rs:50");
    printf("\n\n Enter Your choice  : ");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:
        printf("\nYou have selected Coffee");
        printf("\nEnter The Qty : ");
        scanf("%d",&qty);
        net=net+(qty*15);
        break;
     case 2:
        printf("\nYou have selected Tea");
        printf("\nEnter The Qty : ");
        scanf("%d",&qty);
        net=net+(qty*10);
        break;
     case 3:
        printf("\nYou have selected Cold Coffee");
        printf("\nEnter The Qty : ");
        scanf("%d",&qty);
        net=net+(qty*25);
        break;
     case 4:
        printf("\nYou have selected Milk Shake");
        printf("\nEnter The Qty : ");
        scanf("%d",&qty);
        net=net+(qty*50);
        break;
     default:
          printf("\nInvalid Product Selection");
          break;
 
    }
 
    printf("\nDo You want to continue press 1: ");
    scanf("%d",&i);
    if(i==1)
    {
        goto joes;
    }
     printf("\nTotal amount : %d",net);
     printf("\nThank You Come Again");
    return 0;
}
 
To download raw file Click Here

Output

        MENU CARD
                1.COFFEE        Rs:15
                2.TEA           Rs:10
                3.COLD COFFEE   Rs:25
                4.MILK SHAKE    Rs:50

 Enter Your choice  : 1

You have selected Coffee
Enter The Qty : 3

Do You want to continue press 1: 1

        MENU CARD
                1.COFFEE        Rs:15
                2.TEA           Rs:10
                3.COLD COFFEE   Rs:25
                4.MILK SHAKE    Rs:50

 Enter Your choice  : 4

You have selected Milk Shake
Enter The Qty : 2

Do You want to continue press 1: 2

Total amount : 145
Thank You Come Again

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C