Calculator in C++

1. Introduction

This blog post will guide you through creating a basic calculator in C++. The calculator will be capable of performing addition, subtraction, multiplication, and division.

Calculator in C++

2. Program Steps

1. Display a menu of operations to the user.

2. Take the user's choice of operation.

3. Ask the user for two numbers.

4. Perform the chosen operation on the numbers.

5. Display the result.

6. Loop back to the menu unless the user chooses to exit.

3. Code Program

#include <iostream>
using namespace std;

void displayMenu() {
    cout << "Simple Calculator\n";
    cout << "1. Add\n";
    cout << "2. Subtract\n";
    cout << "3. Multiply\n";
    cout << "4. Divide\n";
    cout << "5. Exit\n";
    cout << "Enter your choice: ";
}

double add(double a, double b) {
    return a + b;
}

double subtract(double a, double b) {
    return a - b;
}

double multiply(double a, double b) {
    return a * b;
}

double divide(double a, double b) {
    if (b == 0) {
        cout << "Error: Division by zero!" << endl;
        return 0;
    }
    return a / b;
}

int main() {
    double num1, num2;
    int choice;

    do {
        displayMenu();
        cin >> choice;

        if (choice >= 1 && choice <= 4) {
            cout << "Enter first number: ";
            cin >> num1;
            cout << "Enter second number: ";
            cin >> num2;
        }

        switch (choice) {
            case 1:
                cout << "Result: " << add(num1, num2) << endl;
                break;
            case 2:
                cout << "Result: " << subtract(num1, num2) << endl;
                break;
            case 3:
                cout << "Result: " << multiply(num1, num2) << endl;
                break;
            case 4:
                cout << "Result: " << divide(num1, num2) << endl;
                break;
            case 5:
                cout << "Exiting program." << endl;
                break;
            default:
                cout << "Invalid choice!" << endl;
        }
    } while (choice != 5);

    return 0;
}

Output:

Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 1
Enter first number: 10
Enter second number: 20
Result: 30
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 2
Enter first number: 20
Enter second number: 10
Result: 10
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 3
Enter first number: 5
Enter second number: 2
Result: 10
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 4
Enter first number: 10
Enter second number: 3
Result: 3.33333
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 5
Exiting program.

Explanation:

1. The program starts by displaying a menu of operations to the user.

2. It reads the user's choice and, based on the selection, prompts the user to enter two numbers.

3. Depending on the operation chosen, the program calls the corresponding function (add, subtract, multiply, divide) to perform the calculation.

4. The result of the operation is then displayed to the user.

5. If the user chooses to divide by zero, an error message is displayed.

6. The program uses a loop to return to the menu after displaying the result, allowing for multiple calculations until the user decides to exit by selecting option 5.

This simple C++ calculator demonstrates basic programming concepts such as functions, loops, and conditional statements.

Comments