Hello, I am learning how to code in C++ and one of my assignments is to make a calculator. Before I turn this in, is there anything that I am doing wrong or
could I make this more efficient /easier. Thank you in advance for your help!
I am using Visual Studio Professional 2013.
P.S. - I was told not to use the "using namespace std;" for this assignment.
I'm guessing you could make it more c++ like. A switch statement would be more optimized than if statements.
I'm going back to old days of C and ansi C. An if statement requires pushing two values onto the stack. A switch statement turn into a jump statement on the assembly level.
I also changed some of the variable names. This makes the code easier to read. It takes more typing, but code is generally read more than it is written.
Also, by putting a break in the if (firstOperand == -1) statement, the need for a loop stopping variable is eliminated, and you don't have to put a bunch of code into an else block, saving a level of indentation.
I agree with booradley60, when you have variables with ambiguous names, it will make it harder to understand when you come back to it, weeks, months or even years later.
For Ch1156 - I haven't learned about switch statements but thats seems to look better!
Also yes I will try the different way of cin statements.
I don't understand the point of the if statement on line 14
For booradley60 - I like your name(to kill a mockingbird). Also yes the while loop does look
promising but I don't understand the point of the if statement on line 12
the if statement on line 14 basically is saying: "If you type in -1, the program will end"
although i did not program a message that informs the user of this, it would be a good idea to do so.
Switch statements are quite easy to learn and are very helpful. basicaly whenever you have more than 2 or 3 if statements, see if you can re write that code into a switch statement.
Remember that names (labels) can be self commenting.
Thus instead of using the label b for an operator maybe use "op" or "opr" or "oper".
You can't use "operator" because it is a reserved word.
And instead of a and c you might use num1 and num2.
I think it looks much more like c++ now. The first looked a lot like BASIC. My problem is my code looks more like c than c++, but thats where I learned.