C++ Code Example: simple calculator

The simple calculator expects user input for the arithmetic operation, and for the two numbers that are to be calculated together. Depending on the arithmetic symbol, a switch statement is used to decide which arithmetic operation is to be performed.

#include <iostream>
using namespace std;

int main() {
    double firstNumber, secondNumber, result;
    char arithmeticOperation;

    cout << "Please enter an arithmetic operation (+, -, *, /): " << endl;
    cin >> arithmeticOperation;
    cout << "Please enter your first number: " << endl;
    cin >> firstNumber;
    cout << "Please enter your second number: " << endl;
    cin >> secondNumber;

    switch (arithmeticOperation) {
    case '+':
        result = firstNumber + secondNumber;
        break;
    case '-':
        result = firstNumber - secondNumber;
        break;
    case '*':
        result = firstNumber * secondNumber;
        break;
    case '/':
        result = firstNumber / secondNumber;
        break;
    default:
        cout << "Unknown arithmetic symbol" << endl;
    }
    
    cout << firstNumber << ' ' << arithmeticOperation << ' ' << secondNumber << " = " << result << endl;
    return 0;
}
Output
Please enter an arithmetic operation (+, -, *, /): 
+
Please enter your first number: 
6
Please enter your second number: 
9
6 + 9 = 15