Menu Driven Program
Menu Driven Program in C++ (3 Ways With Output)
C++ Menu Driven Program Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
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: ";
}
int main() {
int choice;
double num1, num2, result;
do {
displayMenu();
cin >> choice;
if (choice >= 1 && choice <= 4) {
cout << "Enter two numbers: ";
cin >> num1 >> num2;
}
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;
}
} while(choice != 5);
return 0;
}=== MENU === 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit Enter your choice: 1 Enter two numbers: 10 20 Result: 30 === MENU === 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit Enter your choice: 5 Exiting...
Understanding Menu Driven Program
This program teaches you how to create a menu-driven program in C++. A menu-driven program provides a user-friendly interface where users can select options from a menu to perform different operations. This is a common pattern in real-world applications, calculators, and interactive programs.
---
1. What This Program Does
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.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Menu-Driven Programs
Key Components
:
Benefits
:
---
4. 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
:
---
5. Declaring Variables
The program declares:
int choice;
double num1, num2, result;
---
6. Main Loop: do-while
do {
displayMenu();
cin >> choice;
} while(choice != 5);
// ... operations ...How it works
:
Why do-while?
:
---
7. Input Validation
if (choice >= 1 && choice <= 4) {
cin >> num1 >> num2;
}
cout << "Enter two numbers: ";How it works
:
---
8. Switch-Case for Operations
switch(choice) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
} else {
}
break;
case 5:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
cout << "Result: " << result << endl;How it works
:
Division by Zero Check
:
---
9. Other Methods (Mentioned but not shown in code)
Method 2: Using Functions
void add() { /* addition logic */ }
void subtract() { /* subtraction logic */ }
switch(choice) {
// ...
}
// ... other functions ...Method 3: Using Classes
class Calculator {
void add() { /* ... */ }
void subtract() { /* ... */ }
};
---
// ...10. When to Use Menu-Driven Programs
Real-World Applications
:
Educational Purposes
:
---
11. Important Considerations
Input Validation
:
User Experience
:
Error Handling
:
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning program structure, user interaction, switch-case statements, and preparing for building complete interactive applications in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Menu Driven Program
This C++ program is part of the "Application Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.