# First C++ calculator

This is my first C++ program. It would be nice if you can tell me anything I could add or do differently.

I am new to C++ programming and I hope to learn more.

#include <iostream>
using namespace std;

int main()
{
float a, b, result;
char op;

cout << "Enter an operator." << endl;

cin >> op;

cout << "Enter first value." << endl;

cin >> a;

cout << "Enter second value." << endl;

cin >> b;

cout << "----------------------" << endl;

switch (op)
{
case '+':
result = a + b;
cout << result;
break;

case '-':
result = a - b;
cout << result;
break;

case '*':
result = a * b;
cout << result;
break;

case '/':
result = a / b;
cout << result;
break;

default:
cout << "Invalid operation. Program terminated." << endl;
return -1;
}
}

• Welcome to Code Review. I know it's quite obvious from your code what the program does, but would you please state the purpose of the program in the question title as well? That makes it easier for other people to find and review your code.
– Mast
Apr 15, 2021 at 9:22
• When I ran the code I still did not get the answer to the qeustion. So I still don't know what + 1 2 would be as a result. Apr 15, 2021 at 20:03
• stackoverflow.com/questions/5492380/… Apr 16, 2021 at 22:04

there is a lot of thing you can do to further add depth:

• move the printing out of the switch
• display an help with current operators
• implements others operator (pow(), sin(), square(), root()...), you'll then have to check the number of arguments
• use a std::map<string, *functionPointer> to avoid the switch
• implement a parsing function so you can just enter something like "3+2*8", this would require care as you have to check operator precedence, using prefix or postfix representation could help and allow ignoring ")(".

You could try to improve it by adding support for complete expressions meaning that the user should be able to input an expression like "3 + 1" and get the output.

Reverse Polish Notation aka postfix notation, where the operator is put after the operands, ( 3 1 +) is easier to parse than the standard infix notation and very easy to evaluate with an std::stack.

To make the code more re-usable you may want to separate some functionalities using classes. You could create a class Expression which handles parsing, representation, evaluation and conversion to string of a generic expression.

Expression expression = Expression::parsePostfix("3 1 +");

cout << expression.toString() << " = " << expression.evaluate();

• I did avoid talking about more oop concept as OP used fairly simple statementss and no functions (aside pointer). And some of my suggestion are kinda hard to implement for a beginner in C++ (easyer in langage like python). Apr 16, 2021 at 17:18
• I imagined that, there is nothing wrong with your answer, I upvoted it ;) Apr 16, 2021 at 17:35

## Review

Welcome to Code Review. There are few suggestions as below.

### Using namespace

Your code is simple and it seems there is no disadvantage about using namespace std; here. However, be careful about this. If the more libraries you used in the future, the managing complexity you need to check further. Please check this.

### Operator selection

It is simple that using char to store operator like +, -, * and / in your cases. How about the more complex operators (pow(), sin(), square(), root()...) mentioned by jolrael's answer?

### Reduce redundancy

You use many times cout << result;. Actually, this can be moved after switch-case statement.