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

Here, we are going to create a c++ program to implement a simple calculator using the switch case statement.

C++ switch….case statement

The switch statement allows us to execute a block of code among many alternatives.
The syntax of the switch statement in C++ is:
switch (expression)  {
    case constant1:
        // code to be executed if 
        // expression is equal to constant1;
        break;

    case constant2:
        // code to be executed if
        // expression is equal to constant2;
        break;

        .default:
        // code to be executed if
        // expression doesn't match any constant
}

How does the switch statement work?

The expression is evaluated once and compared with the values of each case label.
•    If there is a match, the corresponding code after the matching label is executed. For example, if the value of the variable is equal to constant2, the code after case constant2: is executed until the break statement is encountered.
•    If there is no match, the code after default: is executed.

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

Here we are using the switch...case statement to perform addition, subtraction, multiplication, and division.
How This Program Works
1.    We first prompt the user to enter the desired operator. This input is then stored in the char variable named oper.
2.    We then prompt the user to enter two numbers, which are stored in the float variables num1 and num2.
3.    The switch statement is then used to check the operator entered by the user:
o    If the user enters +, addition is performed on the numbers.
o    If the user enters -, subtraction is performed on the numbers.
o    If the user enters *, multiplication is performed on the numbers.
o    If the user enters /, a division is performed on the numbers.
o    If the user enters any other character, the default code is printed.

Notice that the break statement is used inside each case block. This terminates the switch statement.
If the break statement is not used, all cases after the correct case are executed.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main();

Step 4: Declare a character type variable oper; and two float type variables num1 and                    num2.

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

Step 6: Read the operator into the variable oper;

Step 7: Ask the user to enter two numbers;

Step 8: Read the numbers into the variables num1 and num2;

Step 9: pass the variable oper as the expression of the switch statement. Switch (oper),               and compare with the values of each case label.

Step 10: Define each case label.
              Case ‘+’ num1 + num2; break;
              Case ‘-’ num1 - num2; break;
              Case ‘*’ num1 * num2; break;
              Case ‘/’ num1 / num2; break;

Step 11: Printan error message if the operator dosen’t match any case constant.

Step 12: Exit;
 

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