In this article, we are going to understand Simple C Program to Print Menu with Sub-Menuhere we have a menu and a sub-menu in it. Users can navigate from the main menu to the sub-menu and vice-versa.
Simple Menu program with Sub-Menu in C
We can print simple Menu in C using this program
- do-while : loop to print a Menu until we exit and it is used to print the Menu in our program.
- MAIN MENU
- Please Select one of the following Options :
- Go to SubMenu
- Do Something
- Exit Program
- DoSomething() function : This function is call whenever we select the first option “2” we can customize this as per need
- SubMenu() function: This function is call whenever we select option 1 that is “Go to SubMenu”.We can customize it as per our requirement.It display the Sub-Menu option that includes
- SUB MENU :Please Select one of the following Options :
- Do Something
- Return to Main Menu
- Exit program
- SUB MENU :Please Select one of the following Options :
Simple C Program to Print Menu with Sub-Menu
#include<stdio.h>
#include <stdlib.h>
int SubMenu(void);
int DoSomething();
int main()
{
int mainselect = 0;
startover:
do
{
printf("\n\n MAIN MENU\n");
printf("Please Select one of the following Options :\n");
printf(" 1. Go to SubMenu\n");
printf(" 2. Do Something \n");
printf(" 3. Exit Program\n\n");
printf("\nSelect a Option (1-3):");
scanf("%d", &mainselect);
}while((mainselect < 1) && (mainselect > 4));
switch(mainselect)
{
case 1:
{
int submenuresult = SubMenu();
if(submenuresult == 1 )
goto startover;
}
case 2:
printf("\n");
DoSomething();
goto startover;
case 3:
printf("Exiting Program\n");
exit(0);
return 0;
default:
printf("Invalid Choice\n");
goto startover;
}
return 0;
}
int DoSomething()
{
printf("Doing something. \n");
printf("something Completed. \n");
printf("Retruning after job done. \n");
}
int SubMenu()
{
int onvselect = 0;
SubMenuStart:
do
{
printf("\n SUB MENU\n");
printf("Please Select one of the following Options :\n");
printf(" 1. Do Something\n");
printf(" 2. Return to Main Menu\n");
printf(" 3. Exit program\n");
printf("\nSelect a Option (1-3):");
scanf("%d",&onvselect);
}while((onvselect < 1) && (onvselect > 4));
switch(onvselect)
{
case 1:
printf("\nDoing Something in Submenu");
DoSomething();
goto SubMenuStart;
case 2:
printf("\n");
return 1;
case 3:
printf("\n");
exit(0);
return 0;
default:
printf("Invalid Choice\n");
goto SubMenuStart;
}
goto SubMenuStart;
}
Output
MAIN MENU
Please Select one of the following Options :
1. Go to SubMenu
2. Do Something
3. Exit Program
Select a Option (1-3):1
SUB MENU
Please Select one of the following Options :
1. Do Something
2. Return to Main Menu
3. Exit program
Select a Option (1-3):2
MAIN MENU
Please Select one of the following Options :
1. Go to SubMenu
2. Do Something
3. Exit Program
Select a Option (1-3):2
Doing something.
something Completed.
Retruning after job done.
MAIN MENU
Please Select one of the following Options :
1. Go to SubMenu
2. Do Something
3. Exit Program
Select a Option (1-3):3
Exiting Program