Introduction
In this tutorial, we will create a simple calculator program in C++ that performs basic mathematical operations such as addition, subtraction, multiplication, and division.
A calculator is an essential tool for solving mathematical problems, and this program will allow users to interactively input numbers and choose operations to perform calculations.
Objective
The primary objective of this program is to implement a basic arithmetic calculator that can:
- Perform addition
- Perform subtraction
- Perform multiplication
- Perform division (with error handling for division by zero)
The program will use a simple text-based menu to allow users to select the desired operation, enter operands, and then display the result.
Code Implementation
#include using namespace std; // Function to perform addition float add(float a, float b) { return a + b; } // Function to perform subtraction float subtract(float a, float b) { return a - b; } // Function to perform multiplication float multiply(float a, float b) { return a * b; } // Function to perform division float divide(float a, float b) { if (b != 0) { return a / b; } else { cout << "Error: Division by zero is not allowed!" << endl; return 0; } } int main() { int choice; float num1, num2, result; // Displaying menu to the user cout << "Simple Calculator\n"; cout << "------------------\n"; cout << "Choose an operation:\n"; cout << "1. Add\n"; cout << "2. Subtract\n"; cout << "3. Multiply\n"; cout << "4. Divide\n"; cout << "Enter your choice (1-4): "; cin >> choice; // Checking if the user entered a valid option if (choice < 1 || choice > 4) { cout << "Invalid choice! Please select a number between 1 and 4.\n"; return 1; } // Getting the input numbers from the user cout << "Enter first number: "; cin >> num1; cout << "Enter second number: "; cin >> num2; // Performing the operation based on user choice switch (choice) { case 1: result = add(num1, num2); cout << "Result: " << num1 << " + " << num2 << " = " << result << endl; break; case 2: result = subtract(num1, num2); cout << "Result: " << num1 << " - " << num2 << " = " << result << endl; break; case 3: result = multiply(num1, num2); cout << "Result: " << num1 << " * " << num2 << " = " << result << endl; break; case 4: result = divide(num1, num2); if (num2 != 0) { cout << "Result: " << num1 << " / " << num2 << " = " << result << endl; } break; } return 0; }
Program Explanation
The C++ program begins by including the iostream
header, which is required to handle input and output operations.
The program defines four functions, each corresponding to one of the four arithmetic operations: addition, subtraction, multiplication, and division.
Functions Explained:
- add(float a, float b): This function takes two floating-point numbers and returns their sum.
- subtract(float a, float b): This function subtracts the second number from the first and returns the result.
- multiply(float a, float b): This function multiplies the two input numbers and returns the product.
- divide(float a, float b): This function performs division, checking for division by zero. If the second number is zero, an error message is displayed and the function returns zero.
Main Program Flow:
- The program starts by displaying a simple menu with options to choose the arithmetic operation (addition, subtraction, multiplication, or division).
- The user is prompted to enter the operation choice, and the program checks if the choice is valid (between 1 and 4).
- The program then asks the user to input two numbers for the operation.
- Based on the user’s choice, the corresponding function is called to perform the operation, and the result is displayed on the screen.
How to Run the Program
Follow these steps to run the program on your computer:
- Ensure you have a C++ compiler installed (e.g., GCC, Clang, or MinGW).
- Open your preferred code editor (e.g., Visual Studio Code, Code::Blocks, or Sublime Text) and create a new C++ file (e.g.,
calculator.cpp
). - Copy and paste the provided C++ code into your file.
- Compile the code using your compiler. For example, with GCC, you can use the following command in your terminal or command prompt:
g++ calculator.cpp -o calculator
- Run the compiled program using:
./calculator
- Follow the prompts to input numbers and select the operation. The result will be displayed on the screen.
Conclusion
This simple calculator program provides an easy-to-understand implementation of basic arithmetic operations in C++. It introduces key concepts in C++ such as functions, user input, and conditional statements, which are fundamental to many programming tasks.