C++ Program to Create a calculator using the switch statement.


April 16, 2022, Learn eTutorial
1878

A calculator is a basic program that can able to do some basic mathematical calculations like Addition, Subtraction, Multiplication, and Division. Here, we are going to create a c++ program to implement a simple calculator using the switch case statement.

How does the switch statement work?

The switch is a conditional statement in C++ that helps to change the program flow depending on some condition. We give a condition in the switch and the control of the switch will be transferred to the case statement depending on the condition. 

Note: There must be a default case for the switch, if no case satisfies the switch condition the control jumps to the default case.

C++ Program to create a calculator using the switch statement.

In this C++ program, we use the switch...case statement to perform Addition, Subtraction, Multiplication, and Division depending upon the user input.

How This basic calculator Program Works

  1. We ask the user to input the operation that he wants to do in the calculator
  2. Now we ask the user to input the values which we have to do the operation and store the values
  3. Check the operator that the user enters using a switch case statement:
    • If the user inputs a +, then the switch control is transferred to Addition.
    • If the user inputs a  -, then the control is transferred to the substation statement.
    • If the user inputs a *, then the control is transferred to the multiplication
    • If the user inputs a /, then the control is transferred to division. 
    • If there is no such operator the default case will work.

Note: We have to use the break statement in each case of switch to take the control out of the switch case, else all the switch cases will be executed. 

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: start the main function; int main();

Step 4: Declare the variables of integer type oper, and two float type num1 and num2.

Step 5: Ask the user to input an operator ( +, -, *, / )

Step 6: Read the operator and store it in the variable oper;

Step 7: Ask the user to input two numbers;

Step 8: Read the numbers and store that in the variables num1 and num2;

Step 9: Start the switch statement and check the switch cases with the user input operator.

Step 10: Add the switch cases as
              Case ‘+’ for addition
              Case ‘-’  for substraction
              Case ‘*’ for multiplication
              Case ‘/’ for division (add a break in every case of the switch)

Step 11: Transfer the control to default case if any of the cases match and print an error message

Step 12: Exit;
 


To implement a calculator program in C++, we need to learn the below concepts, we recommend you to read those topics for a better understanding

C++ Source Code

                                          // Program to build a simple calculator using switch Statement
#include <iostream>
using namespace std;

int main() {
    char oper;
    float num1, num2;
    cout << "Enter an operator (+, -, *, /): ";
    cin >> oper;
    cout << "Enter two numbers: " << endl;
    cin >> num1 >> num2;

    switch (oper) {
        case '+':
            cout << num1 << " + " << num2 << " = " << num1 + num2;
            break;
        case '-':
            cout << num1 << " - " << num2 << " = " << num1 - num2;
            break;
        case '*':
            cout << num1 << " * " << num2 << " = " << num1 * num2;
            break;
        case '/':
            cout << num1 << " / " << num2 << " = " << num1 / num2;
            break;
        default:
            // operator is doesn't match any case constant (+, -, *, /)
            cout << "Error! The operator is not correct";
            break;
    }

    return 0;
}
                                      

OUTPUT

Run 1
Enter an operator (+, -, *, /): +
Enter two numbers: 
5
8
5 + 8 = 13
Run 2
Enter an operator (+, -, *, /): *
Enter two numbers: 
2
10
2 * 10 = 20
Run 3
Enter an operator (+, -, *, /): -
Enter two numbers: 
96
52
96 - 52 = 44
Run 4
Enter an operator (+, -, *, /): /
Enter two numbers: 
63
3
63 / 3 = 21