I wrote this simple calculator program in which the user can store a calculation in a variable that was inputted. I do not know how to explain this better so I'll demonstrate with an example. If the input is:
a = 1 + 2
b = 6 - 5
c = 20 * a
d = a / b
exit
Then the output should be:
3
1
60
3
See how the program retains the value of a = 1 + 2
as a = 3
. Then the user can freely use a
in another calculation. For example, 20 * a
. Since a = 3
now, 20 * a
should equal 60
.
However, there is a problem with my program. If I input the following:
a = 1 + 2
b = a + 3
c = a + b
d = 20 * a
Then I get a problem with the output for lines 3 and 4. The output I am getting looks like follows:
3
6
55
As you can see it is saying that a + b = 55
when it should 9
. And when I try to multiply 20 * a
I don't even get an output. I am assuming that there is no output for that last line because it is not even entering the multiplication case in the code. But have no idea where the 55
is coming from.
Here is my code (I know that it is messy but it is because I have tried everything to fix this and I've had no luck):
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <sstream>
#include <string>
using namespace std;
char op='a';
double num1, num2, num3=0;
int sub1, sub2=0;
std::string temp;
int main() {
double var[26];
bool flag = false;
char eq[5];
while (true) {
getline(cin, temp);
if (temp.compare("exit") == 0) {
break;
}
std::stringstream os(temp);
int count = 0;
for(char& c : temp) {
if(c != ' '){
eq[count] = c;
count++;
}
}
/* for(std::string::size_type i = 0; i < temp.size(); ++i){
if(temp[i] != ' '){
eq[count] = temp[i];
count++;
}
}
*/
if(eq[0] >= 97 && eq[0] <= 122){
num1 = eq[2] - 48;
op = eq[3];
num2 = eq[4] - 48;
flag = true;
}
else{
num1 = eq[0] - 48;
op = eq[1];
num2 = eq[2] - 48;
}
switch (op) {
case '+':
if(flag == true){
var[eq[0] - 97] = num1 + num2;
}
if((int)num1 >= 48 && (int)num1 <= 57){
num1 = var[(int)num1 + 48 - 97];
}
if((int)num2 >= 48 && (int)num2 <= 57){
num2 = var[(int)num2 + 48 - 97];
}
cout << num1 + num2 << endl;
break;
case '-':
if(flag == true){
var[eq[0] - 97] = num1 - num2;
}
if((int)num1 >= 48 && (int)num1 <= 57){
num1 = var[(int)num1 + 48- 97];
}
if((int)num2 >= 48 && (int)num2 <= 57){
num2 = var[(int)num2 + 48- 97];
}
cout << num1 - num2 << endl;
break;
case '*':
if(flag == true){
var[eq[0] - 97] = num1 * num2;
}
if((int)num1 >= 48 && (int)num1 <= 57){
num1 = var[(int)num1 + 48- 97];
}
if((int)num2 >= 48 && (int)num2 <= 57){
num2 = var[(int)num2 + 48- 97];
}
cout << num1 * num2 << endl;
break;
case '/':
if(flag == true){
var[eq[0] - 97] = num1 / num2;
}
if((int)num1 >= 48 && (int)num1 <= 57){
num1 = var[(int)num1 + 48- 97];
}
if((int)num2 >= 48 && (int)num2 <= 57){
num2 = var[(int)num2 + 48- 97];
}
cout << num1 / num2 << endl;
break;
case '%':
sub1 = num1;
sub2 = num2;
if(flag == true){
var[eq[0] - 97] = sub1 % sub2;
}
if((int)num1 >= 48 && (int)num1 <= 57){
num1 = var[(int)num1 + 48- 97];
}
if((int)num2 >= 48 && (int)num2 <= 57){
num2 = var[(int)num2 + 48- 97];
}
cout << sub1 % sub2 << endl;
break;
case '^':
if(flag == true){
var[eq[0] - 97] = pow(num1, num2);
}
if((int)num1 >= 48 && (int)num1 <= 57){
num1 = var[(int)num1 + 48- 97];
}
if((int)num2 >= 48 && (int)num2 <= 57){
num2 = var[(int)num2 + 48- 97];
}
cout << pow(num1, num2) << endl;
break;
}
}
return 0;
}
I thought the problem was in the for loop and that is why you see that one for loop is set as a comment. I tried replacing it with another for loop but no luck. I would greatly appreciate some help with this as I have tried numerous things and nothing works. I would put on here things that I have tried but I do not want to make the post any longer. If you need any more information please let me know.