How to Create a Calculator with the switch Statement in C++

By Stephen R. Davis

You can use the switch statement in C++ to make choices between options. The following SwitchCalculator program uses the switch statement to implement a simple calculator:

// SwitchCalculator - use the switch statement to
//                    implement a calculator
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
    // enter operand1 op operand2
    int  nOperand1;
    int  nOperand2;
    char cOperator;
    cout << "Enter 'value1 op value2'n"
         << "where op is +, -, *, / or %:" << endl;
    cin >> nOperand1 >> cOperator >> nOperand2;
    // echo what the operator entered
    cout << nOperand1 << " "
         << cOperator << " "
         << nOperand2 << " = ";
    // now calculate the result; remember that the
    // user might enter something unexpected
    switch (cOperator)
    {
        case '+':
            cout << nOperand1 + nOperand2;
            break;
        case '-':
            cout << nOperand1 - nOperand2;
            break;
        case '*':
        case 'x':
        case 'X':
            cout << nOperand1 * nOperand2;
            break;
        case '/':
            cout << nOperand1 / nOperand2;
            break;
        case '%':
            cout << nOperand1 % nOperand2;
            break;
        default:
            // didn't understand the operator
            cout << " is not understood";
    }
    cout << endl;
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    cout << "Press Enter to continue..." << endl;
    cin.ignore(10, 'n');
    cin.get();
    return 0;
}

This program begins by prompting the user to enter “value1 op value2” where op is one of the common arithmetic operators +, -, *, / or %. The program then reads the variables nOperand1, cOperator, and nOperand2.

The program starts by echoing back to the user what it read from the keyboard. It follows this with the result of the calculation.

Echoing the input back to the user is always a good programming practice. It gives the user confirmation that the program read his input correctly.

The switch on cOperator differentiates between the operations that this calculator implements. For example, in the case that cOperator is ‘+’, the program reports the sum of nOperand1 and nOperand2.

Because ‘X’ is another common symbol for multiply, the program accepts ‘*’, ‘X’, and ‘x’ all as synonyms for multiply using the case “fall through” feature. The program outputs an error message if cOperator doesn’t match any of the known operators.

The output from a few sample runs appears as follows:

Enter 'value1 op value2'
where op is +, -, *, / or %:
22 x 6
22 x 6 = 132
Press Enter to continue . . .
Enter 'value1 op value2'
where op is +, -, *, / or %:
22 / 6
22 / 6 = 3
Press Enter to continue . . .
Enter 'value1 op value2'
where op is +, -, *, / or %:
22 % 6
22 % 6 = 4
Press Enter to continue . . .
Enter 'value1 op value2'
where op is +, -, *, / or %:
22 $ 6
22 $ 6 =  is not understood
Press Enter to continue . . .

Notice that the final run executes the default case of the switch statement since the character ‘$’ did not match any of the cases.