Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! 🚀 🔹Learn advanced coding techniques in C& C++ 🔹Master Linux internals & shell scripting 🔹Deep dive into Data Structures & Algorithms 🔹Explore Embedded Systems & Microcontrollers (8051,UART, RTOS) 🔹Get hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
Menu Driven Calculator in C++ Using Switch Case (Beginner Project)
Introduction
A menu driven calculator is a beginner-friendly C++ project that helps understand conditional statements, loops, and user input handling. This project allows users to perform basic arithmetic operations like addition, subtraction, multiplication, and division using a simple text-based menu. It demonstrates how real-world programs interact with users repeatedly until an exit condition is met.
Algorithm / Logic Explanation
Start the program.
Declare variables for choice and two numbers.
Display a menu with arithmetic operations and exit option.
Ask the user to enter a choice.
If the choice is between 1 and 4, ask the user to enter two numbers.
Use a
switchstatement to perform the selected operation.For division, check if the second number is zero to avoid errors.
Display the result of the operation.
If the user selects Exit, terminate the program.
Repeat the process using a
do-whileloop until Exit is chosen.
#include<iostream>
using namespace std;
int main()
{
int choice;
double a, b;
do
{
cout << "======Basic Calculator=======" << endl;
cout << "1).Addition:" << endl;
cout << "2).Subtraction:" << endl;
cout << "3).Multiplication:" << endl;
cout << "4).Division:" << endl;
cout << "5).Exit:" << endl;
cout << "============================" << endl;
cout << "Enter your choice:" << endl;
cin >> choice;
if (choice >= 1 && choice <= 4)
{
cout << "Enter first Number:" << endl;
cin >> a;
cout << "Enter Second Number:" << endl;
cin >> b;
}
switch (choice)
{
case 1:
cout << "Result:" << (a + b) << endl;
break;
case 2:
cout << "Result:" << (a - b) << endl;
break;
case 3:
cout << "Result:" << (a * b) << endl;
break;
case 4:
if (b == 0)
cout << "Error! Division by zero" << endl;
else
cout << "Result:" << (a / b) << endl;
break;
case 5:
cout << "Exit Successful. Thank you!" << endl;
break;
default:
cout << "!invalid Choice Select Between (1 to 5)" << endl;
}
} while (choice != 5);
return 0;
}
Explanation of Code
The program begins by including the <iostream> header file to handle input and output operations. Inside the main() function, variables are declared to store the user’s choice and the two numbers required for calculation. A do-while loop is used so that the menu is displayed at least once and continues to appear until the user selects the exit option.
The menu is printed using cout, and the user’s choice is read using cin. If the choice corresponds to an arithmetic operation (1 to 4), the program asks the user to input two numbers. The switch statement then checks the selected option and performs the corresponding operation.
For addition, subtraction, and multiplication, the operations are directly performed. In the case of division, an if condition checks whether the second number is zero to prevent division by zero, which would cause a runtime error. If the choice is invalid, a default message is displayed. When the user selects option 5, the program prints an exit message and terminates gracefully.
Edge Cases / Notes
Division by zero is handled safely.
Invalid menu choices are detected and reported.
The program assumes numeric input; non-numeric input may cause unexpected behavior.
Using
doubleallows decimal calculations.
Conclusion
This menu driven calculator project is an excellent way to practice basic C++ concepts such as loops, switch-case statements, and input handling. It forms a strong foundation for developing more advanced console-based applications in the future.
📢 Follow Us for More C & C++ Projects
▶ YouTube 📘 Facebook ✈ TelegramJoin our community for daily programming tips, MCQs, and projects.
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment