Skip to content
← Back to Community
How to make a basic Calculator (C++)
Profile icon
JasonLiu19

First, open a new repl.
Next declare char op, int num1, and int num2.

char op; int num1,num2;

In the main function, we ask the user for input for num1 and num2.

std::cout << "First number\n>>> "; std::cin >> num1; std::cout << "Second number\n>>> "; std::cin >> num2;

Your code should look something like this.
Next, ask the user for one of 5 operators (+,-,*,/,%).

std::cout << "Type an operator\n+)-Addition\n-)- Subtraction\n*)Multiplication\n/)-Division\n%)-Modulus\n>>> "; std::cin >> op;

This should be your program:

#include <iostream> char op; int num1,num2; int main(){ std::cout << "First number\n>>> "; std::cin >> num1; std::cout << "Second number\n>>> "; std::cin >> num2; std::cout << "Type an operator\n+)-Addition\n-)-Subtraction\n*)Multiplication\n/)-Division\n%)-Modulus\n>>> "; std::cin >> op; }

One of possible outputs:

First number >>> 5 Second number >>> 5 Type an operator +)-Addition -)-Subtraction *)Multiplication /)-Division %)-Modulus >>> +

Next, we have to make the perform an operation on num1 and num2.
For this, we will be using a switch statement.
Here is an example:

int num=2; switch(num){ case 1: std::cout << "a"; break; case 2: std::cout << "b"; break; case 3: std::cout << "c"; break default: std::cout << num+1; break;

First, we declare num which stores the number 2.
Next, we define what we are going to perform the switch on, num.
Then we define 4 cases, case 1, case 2, case 3, and default.
The first 3 execute their code if num is equal to 1, 2, or 3 respectively.
If they do, they break out of the switch so the default case doesn't run.
The default case runs if all the other cases were false.
So for our calculator, we will perform a switch on op.
We are going to do addition first.

switch(op){ case '+': std::cout << "\n\n" << num1 << "+" << num2 << "=" << num1+num2; break; }

As you can see, this looks familiar. The only difference is the fact that we put single-quotes around the case. This is because op is a char and not an int.
The rest are pretty similar.

case '-': std::cout << "\n\n" << num1 << "-" << num2 << "=" << num1-num2; break; case '*': std::cout << "\n\n" << num1 << "*" << num2 << "=" << num1*num2; break; case '/': std::cout << "\n\n" << num1 << "/" << num2 << "=" << num1/num2; break; case '%': std::cout << "\n\n" << num1 << "%" << num2 << "=" << num1%num2; break; default: std::cout << "\n\nInvalid operator!"; break;

And we are done!
Improvements
Although our calculator is decent, there is a lot we can improve.
First, you might have noticed that when doing things like 2/5, you get a whole number instead of a decimal.
To fix this, we declare the variables as a double.

double num1,num2;

We find an error if we do this.
To fix this, we declare two new variables with the same values as the doubles and use them only on the modulus operator.

int numx,numy;

We assign the variables values respectively to num1 and num2.
then

std::cout << "\n\n" << num1 << "%" << num2 << "=" << numx%numy;

Next, if someone types an invalid operation, the switch will break and stop the program. You might not want this. How I fix this is to put it in a function, then call it until they type a valid operator.
Cut the switch into here:

bool opick(){ }

Replace all breaks with return true except for the default case, which will be return false.
Then add a while loop in the main function:

while(opick()==false){ opick(); if(opick()==false){ std::cout << "\n\n>>> "; std::cin >> op; } }

This will actually print 3 error messages, but I don't know how to fix.
This should be your final code:

#include <iostream> char op; double num1,num2; int numx,numy; int num=2; bool opick(){ switch(op){ case '+': std::cout << "\n\n" << num1 << "+" << num2 << "=" << num1+num2; return true; case '-': std::cout << "\n\n" << num1 << "-" << num2 << "-" << num1-num2; return true; case '*': std::cout << "\n\n" << num1 << "*" << num2 << "=" << num1*num2; return true; case '/': std::cout << "\n\n" << num1 << "/" << num2 << "-" << num1/num2; return true; case '%': std::cout << "\n\n" << num1 << "%" << num2 << "-" << numx%numy; return true; default: std::cout << "\n\nInvalid operator!"; return false; } } int main(){ std::cout << "First number\n>>> "; std::cin >> num1; numx=num1; std::cout << "Second number\n>>> "; std::cin >> num2; numy=num2; std::cout << "Type an operator\n+)-Addition\n-)-Subtraction\n*)Multiplication\n/)-Division\n%)-Modulus\n>>> "; std::cin >> op; while(opick()==false){ opick(); if(opick()==false){ std::cout << "\n\n>>> "; std::cin >> op; } } }

This is my first tutorial, so if you feel that some parts were not well explained, let me know in the comments!
upvote plz

Voters
Profile icon
JasonLiu19
Comments
hotnewtop
Profile icon
Bookie0

Nice, but:

This will actually print 3 error messages, but I don't know how to fix.

uhhhh try to fix then..

Als you should really add comments everywhere in your code because comments are great and they help users navigate through the code! ;)

but good start!

Profile icon
Gr8est

Dude, don't ask for upvotes. It's like asking people for money.

Profile icon
JasonLiu19

@Gr8est it was sarcasm

Profile icon
Gr8est

@JasonLiu19, oh sry then

Profile icon
Bookie0

cycles ≠ money @Gr8est

Profile icon
Baconman321

@Gr8est It's perfectly fine to ask for cycles. I add "if you don't like it, what can improve?". That way if they like it, I know from cycles (and hopefully comments). If not, I see feedback on what I can improve.
Plus it's fun to have cycles (hehe)