### Help with a calculator

Here is my currently active code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````int num1; int num2; char choice; int answer; char choice2; bool MoveOn; bool ActiveAnswer; // get first number cout << "Enter your first number" << endl; cin >> num1; // get an operator // is it valid? if not, get another operator while (MoveOn = true) { cout << "What would you like to do? +, -, *, or / ?" << endl; //cout << "Press C to clear and start over or X to close the program" << endl; cin >> choice; if (choice == '+') { cout << "Enter your second number" << endl; cin >> num2; answer = num1 + num2; cout << "The answer is: " << answer << endl; MoveOn = true; continue; } else num1 = answer; { if (num1 = answer) num1 = answer; cout << "Enter your second number" << endl; cin >> num2; answer = num1 + num2; cout << "The answer is: " << answer << endl; } }``````

What needs to happen is after reading in the first number, the user selects an operator, then asks for the second number, prints the output, then loops back to the beginning of the while loop to get a operator again (that works), but after that it needs to use the answer from the last statement as the first number for the second operator (doesn't work). I've tried it a few different ways, but none seem to work like they need to. I'm very new to C++ programming, so there's plenty of things that i don't know yet. This IS a class assignment, but i am not asking for whole code to be completed for me. I do want to learn as I'll be taking more classes after this one. So if anyone has some suggestions or hints, i would greatly appreciate it.
I wish I could have corrected this for you but I am not in a suitable coding environment but I know where your problem is and its line 48 or line 33.

`answer = num1 + num2;`

This is your problem. You did fine by putting everything in a while loop which is great but the main problem you have is that. `int num1` is still carrying the value you gave it from the start of the program. Since you're giving `int num2` a new value and since this statement `answer = num1 + num2;` is still inside your loop it keeps doing the same thing which is adding the first value with the new second value. I hope this gives you a new approach to it.
Last edited on
First of all, what is the purpose of line 7?? It's declared but nowhere in the body.
Line 7 was currently not relevant. It was for something i had previously tried, i just hadn't deleted the variable declaration yet. Since it was taking so long for a reply i had attempted to seek help from another source and have successfully got the following:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````int num1; int num2; char Choice; int answer; char exit; bool MoveOn; // get first number cout << "Enter your first number" << endl; cin >> num1; // get an operator // is it valid? if not, get another operator MoveOn = true; while (MoveOn) { cout << "What would you like to do? +, -, *, or / ?" << endl; cout << "Press X to quit, C to clear, or just continue" << endl; cin >> Choice; if (Choice == 'X') MoveOn = false; else if (Choice == 'x') MoveOn = false; else if (Choice == 'C') { cout << "Enter your first number" << endl; cin >> num1; } else if (Choice == 'c') { cout << "Enter your first number" << endl; cin >> num1; } else if (Choice == '+') { cout << "Enter your second number" << endl; cin >> num2; answer = num1 + num2; cout << "The answer is: " << answer << endl; num1 = answer; }``````

So i have solved the problem and a few more that i was having before.
Line 22: you have it listed as a COMMENT. Remove the // and indent with the other cout in line 20, and do the same with line 24.
Line 22 has got a indented comment.
Topic archived. No new replies allowed.