Build your very own calculator in c++
Hey guys I am back with another series of exciting mini project for absolute beginners to start with. These projects will be for those who have just started learning c++ and reached a basic amount of skills and now they want to implement them and build something useful.
Look I know that there are thousands of calculators available in market but the point is here that you will get to know how this simple calculator that you use in your daily life works under the hood and how developers create such useful things. As a developer yourself you are also going to follow the same pattern and create useful tools and softwares in your career. But you have to start small, so a basic calculator can be the ultimate way to get you going. Let's build something great together!
Let's start building
The calculator urges users to write any two numbers with an operator. Then calculation is done according to what numbers and the operator that user enters. If user enters some wrong operator and number combination, the program returns an error. So that going to be the overall functionality of our program.
Here is the code to build the desired program:
#include <iostream>
using namespace std;
int main() {
// Variables to store numbers and operation
double num1, num2;
char operation;
// Display menu
cout << "Simple Calculator\n";
cout << "-----------------\n";
cout << "Available operations: +, -, *, /\n";
// Get user input
cout << "Enter first number: ";
cin >> num1;
cout << "Enter an operator: ";
cin >> operation;
cout << "Enter second number: ";
cin >> num2;
// Perform calculation based on the operation
switch (operation) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
cout << "Error: Division by zero is not allowed!" << endl;
}
break;
default:
cout << "Invalid operator!" << endl;
}
return 0;
}
Now here I'm going to describe how this program is working. This code can be saved in a c++ file. You can name the file as calculator.cpp or basically anything you like but make sure its meaningful. You can compile this program by using g++ calculator.cpp -o calculator.
#include <iostream>
For your cin (input) and cout (output) this header file is included in the code.
using namespace std;
You can use standard library names e.g, cin , cout with the help of this code. It allows you to drop the prefix std::
int main() {
Its the basic gateway of your program as every c++ program executes from this main function
double num1, num2;char operation;
Declaration of variables.
Double used for storage of decimals. Num 1 and num 2 are the two numbers that you will be taking input from a user. Char operator. Used for storage of mathematical operators.
cout << "Simple Calculator\n";
cout << "-----------------\n";
cout << "Available operations:
These display instructions.
cout << "Enter first number: ";
cin >> num1;
cout << "Enter an operator: ";
cin >> operation;
cout << "Enter second number: ";
cin >> num2;
Taking user input,
For the first number,
Then for the mathematical operator,
Then for the second number.
switch (operation) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
cout << "Error: Division by zero is not allowed!" << endl;
}
break;
default:
cout << "Invalid operator!" << endl;
}
Perform calculation.
Switch evaluates the input operator by user, then performs calculation.
### Case '+'
Adds the two numbers, then displays the result.
### Case '-'
Performs the subtraction operation, then displays the result.
### Case '*'
Performs multiplication, then displays the result.
### Case '/'
First checks if one of the numbers is not zero, then performs division; otherwise, displays an error message.
### Default
Runs if some invalid operation is executed by the user.
### return 0;
Shows that the program has ended successfully.
How does it work when you run it
Simple Calculator
-----------------
Available operations: +, -, *, /
### Start....Simple Calculator
-----------------
Available operations: +, -, *, /
Simple Calculator
-----------------
Available operations: +, -, *, /
### User input
Enter first number: 10
Enter an operator: +
Enter second number: 20
### Output
Result: 30
So now you know how a simple calculator is built using C++. I have tried my best to explain every bit of code but still if there is any confusion or something you would like to discuss then you can contact me on my email.
Thanks!
Comments
Post a Comment