Notes
Simple Calculator Program in C++ Using Switch Statement
In this tutorial, we’ll create a basic calculator program that can perform addition, subtraction, multiplication, and division using the switch statement. This program will demonstrate how to use variables, switch statements, user input, and basic arithmetic operations.
Steps to Build the Program:
1. Declare Variables:
We need variables for storing the user input:
- A character variable
opto store the operator. - Two double variables
num1andnum2to store the numbers on which the operation will be performed. - A double variable
resultto store the result of the operation.
char op;
double num1, num2, result;
2. Initial Output:
Display a welcome message to the user at the start of the program. For example, you can print a line of asterisks to make the interface look clean.
std::cout << "********** Calculator **********" << std::endl;
3. Prompt for User Input:
Ask the user for the operator they want to use (either +, -, *, /) and store it in the op variable.
Then, prompt for two numbers (num1 and num2) to perform the operation on.
std::cout << "Enter either addition (+), subtraction (-), multiplication (*), or division (/): ";
std::cin >> op;
std::cout << "Enter number 1: ";
std::cin >> num1;
std::cout << "Enter number 2: ";
std::cin >> num2;
4. Switch Statement:
The switch statement will check the value of op and perform the corresponding operation. For each possible operation, we’ll calculate the result and print it. If the operator entered by the user doesn’t match any of the valid operators, we’ll handle that using the default case.
switch(op) {
case '+': // Addition
result = num1 + num2;
std::cout << "Result: " << result << std::endl;
break;
case '-': // Subtraction
result = num1 - num2;
std::cout << "Result: " << result << std::endl;
break;
case '*': // Multiplication
result = num1 * num2;
std::cout << "Result: " << result << std::endl;
break;
case '/': // Division
if (num2 != 0) { // Prevent division by zero
result = num1 / num2;
std::cout << "Result: " << result << std::endl;
} else {
std::cout << "Error: Division by zero!" << std::endl;
}
break;
default: // Invalid operator
std::cout << "That wasn't a valid operator!" << std::endl;
break;
}
5. Handling Division by Zero:
We need to handle the case where the user tries to divide by zero. In C++, dividing by zero can result in undefined behavior or an error. So, before performing the division, we check if num2 is zero, and if so, display an error message.
if (num2 != 0) {
result = num1 / num2;
std::cout << "Result: " << result << std::endl;
} else {
std::cout << "Error: Division by zero!" << std::endl;
}
6. Program Output:
The program will output the result of the chosen operation or an error message if the operator is invalid. Here’s how the results will look for each operation:
- Addition:
1.23 + 3.14 = 4.37 - Subtraction:
1.23 - 3.14 = -1.91 - Multiplication:
1.23 * 3.14 = 3.8622 - Division:
1.23 / 3.14 = 0.39172 - Invalid Operator: If the user enters something other than
+, , , or/, such asw, the program will output:That wasn't a valid operator!
7. Improving Output:
It’s a good idea to add a message at the end of the program to let the user know the result of their operation. Also, you can use asterisks to frame the final output for a cleaner look.
std::cout << "*************************************" << std::endl;
Full Code Example:
#include <iostream>
#include <cmath> // For math functions if needed
int main() {
char op;
double num1, num2, result;
// Display the program title
std::cout << "********** Calculator **********" << std::endl;
// Ask the user for the operation and numbers
std::cout << "Enter either addition (+), subtraction (-), multiplication (*), or division (/): ";
std::cin >> op;
std::cout << "Enter number 1: ";
std::cin >> num1;
std::cout << "Enter number 2: ";
std::cin >> num2;
// Switch statement to perform operations
switch(op) {
case '+':
result = num1 + num2;
std::cout << "Result: " << result << std::endl;
break;
case '-':
result = num1 - num2;
std::cout << "Result: " << result << std::endl;
break;
case '*':
result = num1 * num2;
std::cout << "Result: " << result << std::endl;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
std::cout << "Result: " << result << std::endl;
} else {
std::cout << "Error: Division by zero!" << std::endl;
}
break;
default:
std::cout << "That wasn't a valid operator!" << std::endl;
break;
}
// End of the program message
std::cout << "*************************************" << std::endl;
return 0;
}
Summary:
- Switch Statements: This program uses a
switchstatement to check for the operator input (+, , ,/) and perform the corresponding arithmetic operation. - Error Handling: It also handles errors, such as invalid operators and division by zero.
- Simple Calculator: The program is a simple calculator where the user enters two numbers and chooses an operation to perform on them.
- Efficiency: The
switchstatement makes the code more readable and avoids the inefficiencies of using multipleif-elsestatements for each operation.
This is a basic and essential project that helps to understand how user input, conditional logic, and arithmetic operations work together in C++.