Skip to content
C++ Better Explained
Go back
C++ Calculator Program: Build One Step by Step
Edit page

C++ Calculator Program: Build One Step by Step

Building a calculator is the classic first real project for C++ beginners. It’s small enough to finish in one sitting, but it touches the key skills you need: reading user input, making decisions, writing functions, and handling errors.

This tutorial builds a calculator in four stages — each one more complete than the last.


Version 1: The Minimal Calculator

The simplest calculator possible — two numbers, one operation:

#include <iostream>
using namespace std;

int main() {
    double a, b;
    char op;

    cout << "Enter: num op num (e.g. 3 + 4): ";
    cin >> a >> op >> b;

    if (op == '+') cout << a + b << endl;
    else if (op == '-') cout << a - b << endl;
    else if (op == '*') cout << a * b << endl;
    else if (op == '/') cout << a / b << endl;
    else cout << "Unknown operator" << endl;

    return 0;
}

Run it:

Enter: num op num (e.g. 3 + 4): 10 / 3
3.33333

This works, but it has a problem: division by zero will crash or produce garbage. Let’s fix that.


Version 2: With Error Handling

#include <iostream>
using namespace std;

int main() {
    double a, b;
    char op;

    cout << "Enter expression (e.g. 3 + 4): ";
    cin >> a >> op >> b;

    switch (op) {
        case '+':
            cout << "Result: " << a + b << endl;
            break;
        case '-':
            cout << "Result: " << a - b << endl;
            break;
        case '*':
            cout << "Result: " << a * b << endl;
            break;
        case '/':
            if (b == 0) {
                cout << "Error: division by zero" << endl;
            } else {
                cout << "Result: " << a / b << endl;
            }
            break;
        default:
            cout << "Unknown operator: " << op << endl;
    }

    return 0;
}

switch is cleaner than a chain of if/else for this kind of menu-style logic.


Version 3: With Functions

Splitting the calculation into a function makes the code easier to test and extend:

#include <iostream>
using namespace std;

double calculate(double a, char op, double b, bool& error) {
    error = false;
    switch (op) {
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/':
            if (b == 0) { error = true; return 0; }
            return a / b;
        default:
            error = true;
            return 0;
    }
}

int main() {
    double a, b;
    char op;
    bool error;

    cout << "Enter expression: ";
    cin >> a >> op >> b;

    double result = calculate(a, op, b, error);

    if (error) {
        cout << "Error: invalid operation or division by zero" << endl;
    } else {
        cout << "Result: " << result << endl;
    }

    return 0;
}

The bool& error parameter is a reference — the function sets it to true if something goes wrong, and the caller checks it. This is a common pattern for reporting errors without exceptions.


Version 4: With a Loop (Run Until Quit)

The most useful version — keeps running until the user exits:

#include <iostream>
using namespace std;

double calculate(double a, char op, double b, bool& error) {
    error = false;
    switch (op) {
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/':
            if (b == 0) { error = true; return 0; }
            return a / b;
        default:
            error = true;
            return 0;
    }
}

int main() {
    char again = 'y';

    do {
        double a, b;
        char op;
        bool error;

        cout << "\nEnter expression (e.g. 5 * 3): ";
        cin >> a >> op >> b;

        double result = calculate(a, op, b, error);

        if (error) {
            if (op == '/')
                cout << "Error: division by zero" << endl;
            else
                cout << "Error: unknown operator '" << op << "'" << endl;
        } else {
            cout << "= " << result << endl;
        }

        cout << "Calculate again? (y/n): ";
        cin >> again;

    } while (again == 'y' || again == 'Y');

    cout << "Goodbye!" << endl;
    return 0;
}

Sample session:

Enter expression (e.g. 5 * 3): 100 / 7
= 14.2857
Calculate again? (y/n): y

Enter expression (e.g. 5 * 3): 9 / 0
Error: division by zero
Calculate again? (y/n): n
Goodbye!
If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Extending the Calculator

Once you have the basic version working, here are natural next steps:

Add more operations:

case '%':
    if (b == 0) { error = true; return 0; }
    return fmod(a, b);  // #include <cmath> for fmod

Add a history:

vector<string> history;
// after each calculation:
history.push_back(to_string(a) + op + to_string(b) + "=" + to_string(result));

Validate input better:

if (cin.fail()) {
    cin.clear();
    cin.ignore(1000, '\n');
    cout << "Invalid input — enter numbers only" << endl;
    continue;
}

Key Concepts Used



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Next Post
C++ Array vs Vector: Which One Should You Use?