Menu-driven Program in C using switch case

In this program, we will understand Menu-driven programs in C using switch cases. In this code, we are using the do-while loop to print the Menu until the user we are selecting the options. Users can exit the Menu by simply selecting option 3.

Printing menu using the function and loop


  • do-while : loop to print a Menu until we exit
  • AddCustomer() function : This function is call whenever we select the first option “AddCustomer” we can customize this as per need
  • updateCustomer() function: This function is call whenever we select option 2 that is “Update customer”.We can customize it as per our reuirment.

Menu-driven Program in C using switch case

#include<stdio.h>
#include <stdlib.h>

int main (void);

int AddCustomer();
int updateCustomer();

int main()
{
    int mainselect = 0;

	startover:
	do
	{
		printf("\n\n MAIN MENU\n");
		printf("Please Select one of the following Options :\n");
		printf("  1. AddCustomer \n");
		printf("  2. UpdateCustomer \n");
		printf("  3. Exit Program\n"); 
		printf("Select a Option (1-3):");
		scanf("%d", &mainselect);
	}while((mainselect < 1) && (mainselect > 4));

    switch(mainselect)
    {
        case 1:
             AddCustomer();
				goto startover;
        case 2:
             printf("\n");
             updateCustomer();
			 goto startover;
        case 3:
             printf("Exiting Program\n");
             exit(0);
             return 0;
        default:
             printf("Invalid Choice\n");
			 goto startover;
    }
	
return 0;
}
int AddCustomer()
{
		printf("\nWe have Selected :AddCustomer \n");
		printf("Please select new Menu:");
}
int updateCustomer()
{
		printf("We have Selected:Update Customer\n");
		printf("Pease select new Menu:");
		
}

Output

 

 MAIN MENU
Please Select one of the following Options :
  1. AddCustomer 
  2. UpdateCustomer 
  3. Exit Program
Select a Option (1-3):1

We have Selected :AddCustomer 
Please select new Menu:

 MAIN MENU
Please Select one of the following Options :
  1. AddCustomer 
  2. UpdateCustomer 
  3. Exit Program
Select a Option (1-3):2

We have Selected:Update Customer
Pease select new Menu:

 MAIN MENU
Please Select one of the following Options :
  1. AddCustomer 
  2. UpdateCustomer 
  3. Exit Program
Select a Option (1-3):