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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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!

Thank you for the reply.
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!).

Your initial response helped me think about the issue, thank you!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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;
}

Topic archived. No new replies allowed.