4
\$\begingroup\$

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;
    }
} 
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Mast
    Apr 15 at 9:22
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 15 at 20:03
  • \$\begingroup\$ stackoverflow.com/questions/5492380/… \$\endgroup\$ Apr 16 at 22:04
3
\$\begingroup\$

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 ")(".
\$\endgroup\$
3
\$\begingroup\$

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.

Slightly more advanced:

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();
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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). \$\endgroup\$
    – jolrael
    Apr 16 at 17:18
  • \$\begingroup\$ I imagined that, there is nothing wrong with your answer, I upvoted it ;) \$\endgroup\$
    – fede. dev
    Apr 16 at 17:35
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.