Storing a number in a simple calculator, then recalling it in an equation

I'm writing a calculator that can store the answer to memory and recall it later. I'm accomplishing this by allowing the user to store the answer when they choose whether or not to go again. I'm having the user type 's' to store the answer to memory. I want them to be able to type in '1 + s' and the program recognize s is the stored number. Any help to make this work correctly would be awesome. Thank you in advance.

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
  #include <iostream>
#include <string>
#include <cmath>
using namespace std;

//Function prototype
int solve(int, int, char);

int main()
{
	//Declare variables
	int solution, num1, num2;
	char oper;
	string again = "y";
	int storedNum;
	//Output
	cout << "------------\n-Calculator-\n------------" << endl;
	cout << "This is a simple calculator. Please" << endl;
	cout << "enter spaces between operators and operands\nExample: 2[space]+[space]3[enter]\n" << endl;
	cout << "Operators: +, -, *, /, ^" << endl;
	while (again == "y" | again == "s")
	{
		cout << "\nEquation: ";
		//Input
		cin >> num1 >> oper >> num2;
		//Solve and output
		solution = solve(num1, num2, oper);
		cout << "Answer: " << solution << endl;
		//Ask to go again
		cout << "\nDo you want to calculate another equation?\n";
		cout << "Type 'y' for yes or any other key to exit.\n";
		cout << "You can also store this number to memory by typing 's'\n";
		cout << "and use it in another equation by typing 's' for one of the operands.\n";
		cin >> again;
		if (again == "s")
			storedNum = solution;
	}
	return 0;
}

int solve(int num1, int num2, char oper)
{
	//Switch oper
	switch (oper)
	{
	case '+':
		return num1 + num2;
	case '-':
		return num1 - num2;
	case '*':
		return num1 * num2;
	case '/':
		return num1 / num2;
	case '^':
		return pow(num1, num2);
	default:
		cout << "\nIncorrect operation!  Try again: ";
		cin >> num1 >> oper >> num2;
		solve(num1, num2, oper);
	}
}
My logic is to do something like this:
1
2
3
4
5
6
//Input
cin >> num1 >> oper >> num2;
if (num1 == 's')
	num1 = storedNum;
if (num2 == 's')
	num2 = storedNum;

but when I try to run it like this, the program stores the number correctly but then when I try to use it in an equation the program gets stuck in an infinite loop
The problem is that num1 and num2 are of type int, so they cannot contain 's'.
So, when you try to read with cin, and it encounters an 's', it cannot read it, and the read fails (num1 and num2 are probably left unchanged in case of fail). Then the program wil try to read 's' again in the 'default' case, but it fails to read it again for the same reason, and then you have programmed it to keep trying again and again, but it can never read an 's' into a variable of type 'int'.

So, you have to add two new variables: num1str and num2str of type string. Read the data with cin into those variables. Then, you can check whether they contain a number or an 's'. If they contain a number, convert the string to int with

num1 = std::stoi(num1str);
num2 = std::stoi(num2str);
Last edited on
Topic archived. No new replies allowed.