Hey guys, I'm programming a calculator in my c++ class and the assignment is as follows: Write a program, which will act as a simple four-function calculator. That is it will read a number, read an operator, read another number, then do the operation. The calculator works with integers and uses four functions: +, -, *, and /. After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation. If the user enters a C the result is cleared and then the user starts entering a new number. If the user enters an X, the calculator is turned off. The various input values (i.e. numbers, operators, commands) will be followed by the ENTER key. Your program should prompt the user on what the user is to do. The commands C and X may be entered in place of an operator.
This what I got so for but my problem is with the getting the total/result to display.
I think you need to pick your instructions apart into smaller pieces because you are missing some important parts in your program. Like you do no actual calculations with what you enter and a choice of "C" clears nothing, but you ask for the first number when you should be using the result of the last calculation for the first number.
Two small things I see: line 5 should be int main(). "void" is no longer an accepted return value for "main". And line 51 is missing return 0;.
As Handy Andy pointed out, you are not actually doing any calculations. I might suggest a switch statement:
41 42 43 44 45 46
switch (op)
{
case'+': // Do addition
break;
// Repeat for other operators
}
Line 43: Your boolean logic is faulty. If op is '+', then op != '-' will be true making the entire condition true. In fact, the if portion is not needed since you've already checked the valid operators at line 35. Only the else is needed.