Menu Driven Program in C++
Build a menu-driven calculator in C++ with a do-while loop, a displayMenu function, and switch. Learn why do-while fits menus and how choice 5 exits.
1#include <iostream>2using namespace std;34void displayMenu() {5 cout << "\n=== MENU ===" << endl;6 cout << "1. Add" << endl;7 cout << "2. Subtract" << endl;8 cout << "3. Multiply" << endl;9 cout << "4. Divide" << endl;10 cout << "5. Exit" << endl;11 cout << "Enter your choice: ";12}1314int main() {15 int choice;16 double num1, num2, result;1718 do {19 displayMenu();20 cin >> choice;2122 if (choice >= 1 && choice <= 4) {23 cout << "Enter two numbers: ";24 cin >> num1 >> num2;25 }2627 switch(choice) {28 case 1:29 result = num1 + num2;30 cout << "Result: " << result << endl;31 break;32 case 2:33 result = num1 - num2;34 cout << "Result: " << result << endl;35 break;36 case 3:37 result = num1 * num2;38 cout << "Result: " << result << endl;39 break;40 case 4:41 if (num2 != 0) {42 result = num1 / num2;43 cout << "Result: " << result << endl;44 } else {45 cout << "Error: Division by zero!" << endl;46 }47 break;48 case 5:49 cout << "Exiting..." << endl;50 break;51 default:52 cout << "Invalid choice!" << endl;53 }54 } while(choice != 5);5556 return 0;57}
Sample Output
Result: 30
Case 1 added 10 and 20.
Exiting...
Choice 5 ends the loop.
In this one
- Why a do-while loop shows the menu at least once
- How a helper function keeps the menu text out of main
- How switch handles each menu option
- How to skip reading numbers for Exit and invalid choices
How it works
- 1
displayMenu prints the options.
- 2
Read choice (1).
- 3
For choices 1–4, read two numbers (10 20).
- 4
switch runs case 1 and prints 30.
- 5
The loop repeats until choice is 5.
Edge cases
Letters as choice
Type x at the menu.
cin fails and choice is set to 0, so "Invalid choice!" prints. The stream stays failed, so the loop spins forever printing the menu. Call cin.clear() and cin.ignore() to recover.
Divide by zero
Choose 4 and enter 5 0.
The guard prints an error and the menu returns instead of producing inf.
What's going on
The program creates an interactive menu system that allows users to:
The program runs in a loop, displaying the menu repeatedly until the user selects the exit option.
Header File Used
#include <iostream>
This header provides:
Understanding Menu-Driven Programs
Key Components:
Benefits:
Function: displayMenu()
void displayMenu() {
cout << "\n=== MENU ===" << endl;
cout << "1. Add" << endl;
cout << "2. Subtract" << endl;
cout << "3. Multiply" << endl;
cout << "4. Divide" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
}
How it works:
Declaring Variables
The program declares:
int choice;
double num1, num2, result;
Main Loop: do-while
do {
displayMenu();
cin >> choice;
// ... operations ...
} while(choice != 5);
How it works:
Why do-while?:
Input Validation
if (choice >= 1 && choice <= 4) {
cout << "Enter two numbers: ";
cin >> num1 >> num2;
}
How it works:
Switch-Case for Operations
switch(choice) {
case 1:
result = num1 + num2;
cout << "Result: " << result << endl;
break;
case 2:
result = num1 - num2;
cout << "Result: " << result << endl;
break;
case 3:
result = num1 * num2;
cout << "Result: " << result << endl;
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
cout << "Result: " << result << endl;
} else {
cout << "Error: Division by zero!" << endl;
}
break;
case 5:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
How it works:
Division by Zero Check:
Other Methods (Mentioned but not shown in code)
Method 2: Using Functions
void add() { /* addition logic */ }
void subtract() { /* subtraction logic */ }
// ... other functions ...
switch(choice) {
case 1: add(); break;
case 2: subtract(); break;
// ...
}
Method 3: Using Classes
class Calculator {
void add() { /* ... */ }
void subtract() { /* ... */ }
// ...
};
When to Use Menu-Driven Programs
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
How many times does a do-while body run if the condition is false from the start?
Pick an answer to check it.
Question 2
What happens on choice 5?
Pick an answer to check it.
Questions people ask
Why do-while instead of while?
The menu must appear before the first choice exists. do-while runs the body once before testing.
Why check 1–4 before reading numbers?
Exit and invalid choices do not need numbers, so asking would confuse the user.
How do I add an option?
Add a line to displayMenu, a new case, and update the range check.