Simple MENU Program in C++

C-Plus-Plus-Programs

In this post, we are going to understand the Simple MENU Program in C++. We will use a switch statement to display the menu in C++ As you do in other GUI languages. We are using the switch statement in the Main() function and displayMenu() function some custom functions inside it to display MENUS.

In this program code, we are using function the displaymenu() to display a MENU on Console.

5 Custom function used in displaymenu()


These are the 5 functions we are using to display different MENU as per user input. We can change the number of many MENUS as per need.

  • void Addstudent() : To add a new student
  • void DeleteStudent() : To delete an existing student.
  • void updatestudent() : To update a student record.
  • void ViewAllStudentRecords() : To view records of all students,
  • void FindStudent() : To find a studnet by ID.

Simple Program to Display Menu in C++


This is full code to display MENU in C++

C++ Program to Display Menu

#Source Code to display Menu in C++

#include <iostream>
#include<string>
using namespace std;
//Code by devenum.com
void displaymenu()
{ cout<<"===================================================== \n";
cout<<" \t\tMENU \t \n ";
cout<<"===================================================== \n";
cout<<" 1.Add student records\n";
cout<<" 2.Delete student records\n";
cout<<" 3.Update student records \n";
cout<<" 4.View all student records \n";
cout<<" 5.Find a student by ID \n";
}//Code by devenum.com
void Addstudent()
{
cout<< "you have selected add student : \n";
}
void DeleteStudent()
{
cout<< "you have selected delete student \n";
}
void updatestudent()
{
cout<< "you have selected update student 3\n";
}
void ViewAllStudentRecords()
{
cout<< "View all student records 4\n";
}
void FindStudent()
{
cout<< "you have selected Find a stduent \n";
}

//Code by devenum.com

int main()
{ 

string st[20];
int itemcount = 0;

#function to display Menu

displaymenu(); 
int yourchoice;
string confirm;
do
{ cout<<"Enter your choice(1-5):";
cin>>yourchoice;
switch (yourchoice)
{
case 1: Addstudent(); break;
case 2: DeleteStudent();break;
case 3: updatestudent(); break;
case 4: ViewAllStudentRecords(); break;
case 5: FindStudent();break;
default: cout<<"invalid"; break;
}
cout<<"Press y or Y to continue:";
cin>>confirm;
} while (confirm == "y" || confirm == "Y");
return 0;
}

Output

===================================================== 

 		MENU 	 

 ===================================================== 
 1.Add student records

 2.Delete student records

 3.Update student records 

 4.View all student records 

 5.Find a student by ID 

Enter your choice(1-5):1

you have selected add student : 

Press y or Y to continue:^A

How does it works


Inside the Main() function we are using the switch statement to display the MENU as per user input.

We are asking users to enter the choice for MENU to display. As per user input Menu from 1 to 5 will select.

  • if user Input 1 then Addstudent menu will be select.
  • if user input 2 then DeleteStudent menu will be select.
  • if user Input 3 then updatestudent menu will be select.
  • if user input 4 then ViewAllStudentRecords menu will be select.
  • if user Input 5 then FindStudent menu will be select.

Then finally selected will display on the console.