### Storing last result in a calculator

Hi all,

I am very new to C++.
This question is relating to my Coursework, I am not seeking a full solution but a step in the right direction.
I can not find anything online for this (may be searching the wrong terms?).

I have created a simple calculator but now need it to store the last result to be called if needed. So if the user wanted 3+3, I would like the answer 6 to be stored, the user can then retrieve the result if needed.

Thank you for any help offered!

My current code is below:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````int op; float numA, numB, result; int main() { cout << "Calculator V1\n By Jolene Stanley\n\n"; while (true) { cout << "1. Add\n" << "2. Subtract\n" << "3. Divide\n" << "4. Multiply\n" << "5. Square Root\n" << "6. Power\n" << "7. Exit\n\n" << "Please choose an option "; //Creating an exit option for the user cin >> op; if (op == 7) break; if (op == 5) { cout << "\nPlease enter a number to Square Root: "; cin >> numA; result = sqrt(numA); cout << "\nThe Square Root of " << numA << " is: " << result << "\n" << endl; } //Obtaining user input cout << "\nPlease enter the first number: "; cin >> numA; cout << "Please enter the second number: "; cin >> numB; //Switch statement provides a result depending on input and outputs to the screen switch (op) { case 1: result = numA + numB; cout << "The answer is: " << result << "\n\n"; break; case 2: result = numA - numB; cout << "The answer is: " << result << "\n\n"; break; case 3: result = numA / numB; cout << "The answer is: " << result << "\n\n"; break; case 4: result = numA*numB; cout << "The answer is: " << result << "\n\n"; break; case 5: result = sqrt(numA); cout << "The answer is: " << result << "\n\n"; break; case 6: result = pow(numA, numB); cout << "The answer is: " << result << "\n\n"; break; } } return 0; }``````

Last edited on
Hey. Please edit your post and use code tags so it is more clear - http://www.cplusplus.com/articles/jEywvCM9/

Aren't you already storing the result in the variable "result"?

`result = numA + numB;`

Do you want to store every single result, like a history of all the results, or what exactly did you have in mind?
Apologies, I am new to this. I have updated the post.

I only need to store the last result and then allow the user to recall it when required.

I hope that makes sense.

I guess I can use the result variable to do this, I did not think of that!

I have edited the code so the result variable is used.
This worked, I just need to get it so it doesn't prompt the user to enter first and second number still (Same issue with the Square root!).

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` ``````int op; float numA, numB, result; int main() { cout << "Calculator V1\n By Jolene Stanley\n\n"; while (true) { cout << "1. Add\n" << "2. Subtract\n" << "3. Divide\n" << "4. Multiply\n" << "5. Square Root\n" << "6. Power\n" << "7. Recall last result\n" << "8. Exit\n\n" << "Please choose an option "; //Creating an exit option for the user cin >> op; if (op == 8) break; if (op == 5) { cout << "\nPlease enter a number to Square Root: "; cin >> numA; result = sqrt(numA); cout << "\nThe Square Root of " << numA << " is: " << result << "\n" << endl; } //Obtaining user input cout << "\nPlease enter the first number: "; cin >> numA; cout << "Please enter the second number: "; cin >> numB; //Switch statement provides a result depending on input and outputs to the screen switch (op) { case 1: result = numA + numB; cout << "The answer is: " << result << "\n\n"; break; case 2: result = numA - numB; cout << "The answer is: " << result << "\n\n"; break; case 3: result = numA / numB; cout << "The answer is: " << result << "\n\n"; break; case 4: result = numA*numB; cout << "The answer is: " << result << "\n\n"; break; case 5: result = sqrt(numA); cout << "The answer is: " << result << "\n\n"; break; case 6: result = pow(numA, numB); cout << "The answer is: " << result << "\n\n"; break; case 7: cout << "The last result was: " << result << "\n\n"; break; } } return 0; }``````