Basic Calculator

Program to create a basic calculator with arithmetic operations

C++Beginner
C++
#include <iostream>
using namespace std;

int main() {
    char op;
    double num1, num2, result;
    
    cout << "Enter operator (+, -, *, /): ";
    cin >> op;
    
    cout << "Enter two numbers: ";
    cin >> num1 >> num2;
    
    switch(op) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            if (num2 != 0) {
                result = num1 / num2;
            } else {
                cout << "Error: Division by zero!" << endl;
                return 1;
            }
            break;
        default:
            cout << "Error: Invalid operator!" << endl;
            return 1;
    }
    
    cout << num1 << " " << op << " " << num2 << " = " << result << endl;
    
    return 0;
}

Output

Enter operator (+, -, *, /): *
Enter two numbers: 5 4
5 * 4 = 20

Basic Calculator in C++

This program creates a basic calculator that performs arithmetic operations (addition, subtraction, multiplication, division) on two numbers. It demonstrates the switch statement, which is perfect for handling multiple cases based on a single value. The program also includes error handling for division by zero and invalid operators.

What This Program Does

The program:

  • Takes an arithmetic operator (+, -, *, /) as input
  • Takes two numbers as input
  • Performs the corresponding arithmetic operation
  • Displays the result
  • Handles errors (division by zero, invalid operator)

This is a fundamental program that teaches:

  • Switch statement for multiple conditions
  • Character input handling
  • Error handling and validation
  • Basic arithmetic operations

Understanding the Switch Statement

switch(op)

The switch statement is perfect for handling multiple cases based on a single value.

Syntax:

cpp
switch (variable) {
    case value1:
        // code for value1
        break;
    case value2:
        // code for value2
        break;
    default:
        // code if no case matches
}

Why use switch instead of if-else?

  • Cleaner and more readable for multiple conditions
  • More efficient (compiler can optimize)
  • Easier to add new operations

Error Handling

Division by Zero:

  • Check if num2 != 0 before dividing
  • Print error message and exit if divisor is zero

Invalid Operator:

  • Use default case to handle invalid operators
  • Print error message and exit

Summary

  • Switch statement handles multiple cases based on operator
  • Each case performs corresponding arithmetic operation
  • break; prevents falling through to next case
  • Error handling for division by zero and invalid operators
  • double type allows decimal number calculations
  • return 1; indicates error, return 0; indicates success

This program teaches:

  • Switch statement for multiple conditions
  • Character input handling
  • Error handling and validation
  • Arithmetic operations in programming
  • Program exit codes