#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;
}Output
=== 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...
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:
- View a menu of available operations
- Select an option by entering a number
- Perform the selected operation (Add, Subtract, Multiply, Divide)
- Continue until the user chooses to exit
The program runs in a loop, displaying the menu repeatedly until the user selects the exit option.
2. Header File Used
#include <iostream>
This header provides:
- cout for displaying output
- cin for taking input from the user
3. Understanding Menu-Driven Programs
Key Components:
- Menu display function
- User input for choice
- Switch-case for option handling
- Loop to keep program running
- Exit condition
Benefits:
- User-friendly interface
- Clear organization of operations
- Easy to extend with new options
- Professional program structure
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:
- Displays all available options
- Numbers (1-5) represent different operations
- Clear formatting for readability
- Called before each user input
5. Declaring Variables
The program declares: int choice; double num1, num2, result;
- choice stores the user's menu selection (1-5)
- num1, num2 store the two numbers for operations
- result stores the calculation result
- double is used to handle decimal numbers
6. Main Loop: do-while
do { displayMenu(); cin >> choice; // ... operations ... } while(choice != 5);
How it works:
- do-while ensures menu displays at least once
- Loop continues while choice is not 5 (Exit)
- Menu is displayed before each iteration
- User can perform multiple operations
Why do-while?:
- Guarantees menu is shown at least once
- Better user experience than while loop
- Natural flow for menu-driven programs
7. Input Validation
if (choice >= 1 && choice <= 4) { cout << "Enter two numbers: "; cin >> num1 >> num2; }
How it works:
- Only asks for numbers if operation needs them
- Options 1-4 require two numbers
- Option 5 (Exit) doesn't need numbers
- Prevents unnecessary input prompts
8. 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:
- Each case handles one menu option
- Case 1-4: perform arithmetic operations
- Case 4: includes division by zero check
- Case 5: exit message
- default: handles invalid input
Division by Zero Check:
- Important safety feature
- Prevents program crash
- Provides user-friendly error message
9. 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; // ... }
- Each operation in separate function
- More modular and organized
- Easier to maintain and test
Method 3: Using Classes
class Calculator { void add() { /* ... / } void subtract() { / ... */ } // ... };
- Object-oriented approach
- Encapsulates operations
- Better for complex programs
10. When to Use Menu-Driven Programs
Real-World Applications:
- Calculators and tools
- Database management systems
- File management programs
- Interactive applications
Educational Purposes:
- Learning program structure
- Understanding user interaction
- Practicing switch-case statements
- Building complete programs
11. Important Considerations
Input Validation:
- Check for valid menu choices
- Validate numeric input
- Handle division by zero
- Provide clear error messages
User Experience:
- Clear menu formatting
- Informative messages
- Easy exit option
- Consistent interface
Error Handling:
- Invalid choices
- Invalid input types
- Division by zero
- Edge cases
12. return 0;
This ends the program successfully.
Summary
- Menu-driven programs provide user-friendly interactive interfaces.
- do-while loop ensures menu displays at least once.
- Switch-case handles different menu options efficiently.
- Input validation prevents errors and improves user experience.
- Division by zero check is essential for safety.
- Functions and classes can organize operations better.
- Understanding menu-driven structure is essential for real-world applications.
This program is fundamental for beginners learning program structure, user interaction, switch-case statements, and preparing for building complete interactive applications in C++ programs.