Problem with calculator using a linked list stack

I am having trouble with a calculator class where I need to use a stack data structure to evaluate a function inputted by the user. I also had to implement the stack myself, I did so by using a linked list.

The following is my stack class
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
template <typename DataType>
class Stack {
private:
	struct Node
	{
		DataType data;
		Node* next;
	};
	Node* head;
public:
	Stack() { head = NULL; }
	void push(DataType element)
	{
		Node* newNode = new Node();
		newNode->data = element;
		newNode->next = head;
		head = newNode;
	}
	void pop()
	{
		Node* temp = head;
		head = head->next;
		delete temp;
	}

	DataType top()
	{
		return head->data;
	}
	bool empty()
	{
		if (head == NULL)
			return true;
		else 
			false;
	}
};


This is the function where I am having trouble evaluating the expression
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
int eval()
{
	int currentNumber = 0;
	int result = 0;

	for (size_t i = 0; infix.length(); i++)
	{
		if (isOperand(infix[i]) == true)
		{
			std::cout << "isOperand Called\n";
			currentNumber = (currentNumber * 10) + (int)(infix[i] - '0');
		}

		else if (isOperator(infix[i]) == true)
		{
			std::cout << "isOperator Called\n";
			if (infix[i] == '(')
			{
				std::cout << "( Called\n";
				operators.push(infix[i]);
				currentNumber = 0;
			}

			else if (operands.empty() == true)
			{
				std::cout << "Empty Operand Called\n";
				operands.push(currentNumber);
				operators.push(infix[i]);
				currentNumber = 0;
			}

			else if (infix[i] == ')')
			{
				std::cout << ") Called\n";
				operands.push(currentNumber);
				while (operators.top() != '(')
				{
					std::cout << "While operators.top Called\n";
					infix[i] = operators.top();
					operators.pop();
					currentNumber = operands.top();
					operands.pop();
					int previous = operands.top();
					operands.pop();
					currentNumber = operate(previous, currentNumber, infix[i]);
					operands.push(currentNumber);
				}
				operators.pop();
				operands.pop();
			}
		}

		else if (isSpace(infix[i]) == true)
			continue;

		else
		{
			std::cout << "Else Called\n";
			char previous = operators.top();
			if (precedence(infix[i]) > precedence(previous))
			{
				std::cout << "Higher precedence Called\n";
				operands.push(currentNumber);
				operators.push(infix[i]);
				currentNumber = 0;
			}
			else
			{
				std::cout << "Lower precedence Called\n";
				int previousNumber = operands.top();
				operands.pop();
				int previousOperator = operators.top();
				operands.pop();
				previousNumber = operate(previousNumber, currentNumber, previousOperator);
				operands.push(previousNumber);
				operators.push(infix[i]);
				currentNumber = 0;
			}
		}
	}

	while (operators.empty() != true)
	{
		std::cout << "Empty Operators Called\n";
		int previous = operands.top();
		operands.pop();
		char symbols = operators.top();
		operators.pop();
		currentNumber = operate(previous, currentNumber, symbols);
	}

	return currentNumber;
}


The std::cout's are from when I was trying to debug and figure out the problem. When doing this, I found that whenever I would press enter or a space, that was when my function would catch an error. This always happens after the else is called. I get the error in my Stack class in the pop() function that states 'head is a null pointer'. I've tried to make a function called isSpace and put it in between the else function so that if it is a space that it will just continue and ignore the else statement, but that is not working. I am not sure what I could do to fix the problem. Please help!
Last edited on
how did you read into infix?
is it a getline?
if so, are you doing any cin's as well? Mixing the two can cause bugs if not very careful; there are many posts on this issue here and on the web on handling it if you are doing this.
Last edited on
Line 6 is for (size_t i = 0; infix.length(); i++)
The loop continues until infix.length() == 0. Is that what you intended?

Line 48 pops the '(' after you evaluate the expression in the parentheses. That's good.
But why do you pop an operand at line 49? Isn't that just popping the value of the expression?

Line 14 checks for an operator but it only handles '(' and ')'. Should that handle +, -, *, / also? Rght now those perators seem to be handled in the else at line 56.

when processing an operator, the code executes the operator at the top of the stack if it has lower precedence than the current one. That seems confusing, but I guess it's okay if lower numerical precedence means that it should be performed first.

You'll get much more valuable help from us if you can post code that will compile along with the input that's giving you problems.
@jonnin
I did use a getline, and no I am not using any cin's. I only have one input from the user and then it loops back to the beginning until the user enters a sentinel value to exit the loop.

@dhayden
No that is not what I intended! I forgot the i < !
This solved the error that I was receiving initially, but now that the code is running, I am getting wrong results in the calculation.

The reason that I pop an operand at 49 is because I would have two copies of that value , I would have one in the stack and another in the currenNumber variable. I'm not sure if I am tracing that correctly, but I think that is correct? I tried to comment that section out to see if it would give me the correct calculation, but unfortunately it does not.

On line 14 it checks my isOperator function that does include those variables
1
2
3
4
5
6
7
8
bool isOperator(char op)
	{
		if (op == '+' || op == '-' || op == '*' || op == '/' || 
op == '(' || op == ')' || op == '^')
			return true;
		else
			return false;
	}

These are other functions I used in this program
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
bool isSpace(char character)
{
	if (character == ' ' || character == '\r' || character == '\n')
		return true;
	else
		return false;
}
bool isOperand(char number)
{
	if (number >= '0' && number <= '9')
		return true;
	else
		return false;
}
int precedence(char character)
{
	if (character == '(' || character == ')' || character == '[' || character == ']' || character == '{' || character == '}')
		return 4;
	else if (character == '^')
		return 3;
	else if (character == '*' || character == '/')
		return 2;
	else if (character == '+' || character == '-')
		return 1;
	else 
		return -1;
}
int operate(int valueOne, int valueTwo, char op)
{
	if (op == '+')
		return valueOne + valueTwo;
	if (op == '-')
		return valueOne - valueTwo;
	if (op == '*')
		return valueOne * valueTwo;
	if (op == '/')
		return valueOne / valueTwo;
	if (op == '^')
		return (int)pow(valueOne, valueTwo);
}


And this is my source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	std::string userInput, sentinel = "0";
	std::cout << "Enter 0 to exit the program.\n\n";

	while (userInput != sentinel)
	{
		if (userInput != "0")
		{
			std::cout << "Enter an expression: ";
			std::getline(std::cin, userInput);
			Calculator userExpression(userInput);
			std::cout << "\nResult: " << userExpression.eval() << "\n\n";
		}
	}

	system("pause");

	return 0;
}

Last edited on
For simplicity, change Stack::pop() to return the value that was on top of the stack. That way you don't have to call top() and pop() every time you want to consume a value from the stack.

Inside eval, when you see a digit, read the entire number and push it on the operand stack. That way the operands are always on the stack. You're current code keeps trying to use currentNumber when it should really be using the top of the stack.

Here is what I ended up with for eval(). I have removed the code and just left the comments. See if you can fill in the code:
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
int
Calculator::eval()
{
    for (size_t i = 0; i < infix.length(); i++) {
	// For debugging, I added a print() method to Stack. You might do this too.
	cout << "next char is " << infix[i] << '\n';;
	cout << "operator stack:\t";
	operators.print(cout);
	cout << "operand stack:\t";
	operands.print(cout);
	cout << '\n';


	if (isOperand(infix[i]) == true) {
	    // read the entire number and push it on the operand stack
	    // When done, make sure that i refers to the last character
	    // of the number so that "i++" in the "for" loop above will
	    // set it the next character
	}

	else if (infix[i] == '(') {
	    // Push '(' on the operator stack
	} else if (infix[i] == ')') {
	    // while the top of the operator stack isn't '(':
	    //     pop two operands
	    //     pop one operator
	    //     push operand1 <operator> operand2 on the operand stack
	    //
	    // After the while loop, '(' will be at the top of the operator
	    // stack. Pop it off.
	} else if (isSpace(infix[i]) == true) {
	    // Do nothing
	    continue;
	} else if (isOperator(infix[i])){
	    // If the operator stack is empty or if this operator has
	    // higher precedence, then push the operator.
	    // Otherwise you need to perform the queued operation.
	    // Pop 2 operands and an operator, and push
	    // operand1 <oper> operand2 on the operand stack.
	}
    }

    // Perform whatever operations are still on the stack

    // Finally, the top of the operand stack should contain the evaluated
    // expression. Pop it and return that value.
}

@dhayden
Should I completely remove currentNumber? Or could I use it just when getting the entire number? This is what I did, I tried to follow your comments to the best of my ability :

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
int eval()
{
	int currentNumber = 0;

	for (size_t i = 0; i < infix.length(); i++)
	{
		std::cout << "Next char is " << infix[i] << '\n';;
		std::cout << "Operator stack: ";
		operators.print();
		std::cout << "Operand stack: ";
		operands.print();
		std::cout << '\n';

		if (isOperand(infix[i]) == true)
		{
			currentNumber = (currentNumber * 10) + (int)(infix[i] - '0');
			if (isOperand(infix[i + 1]) == false || isSpace(infix[i + 1]) == true)
			{
				operands.push(currentNumber);
				std::cout << "Pushed " << currentNumber << "\n";
				currentNumber = 0;
			}
		}

		else if (infix[i] == '(')
			operators.push(infix[i]);
			
		else if (infix[i] == ')')
		{
			while (operators.top() != '(')
			{
				std::cout << "Top is called\n";
				int valueOne = operands.pop();
				int valueTwo = operands.pop();
				char op = operators.pop();
				operands.push(operate(valueOne,valueTwo,op));
			}
			operators.pop();
		}

		else if (isSpace(infix[i]) == true)
			continue;

		else if (isOperator(infix[i]) == true)
		{
			if (operators.empty() == true || precedence(infix[i]) > precedence(operators.top()))
			{
				std::cout << "Top is called\n";
				operators.push(infix[i]);
			}
			else
			{
				int valueOne = operands.pop();
				int valueTwo = operands.pop();
				char op = operators.pop();
				operands.push(operate(valueOne, valueTwo, op));
			}
		}
	}

	while (operators.empty() != true)
	{
		int valueOne = operands.pop();
		int valueTwo = operands.pop();
		char op = operators.pop();
		operands.push(operate(valueOne, valueTwo, op));
	}

	return operands.pop();
}


However, I think I did the closing parenthesis wrong, every time I run my code with parenthesis, I run into the same error I did at the beginning when I would get an error saying that head was a NULL pointer. Also, when doing simple evaluations like 12 / 3 , I am not getting the correct answer. I only get the last number that was pushed.

Also, I am sure this is not as important, but when I print out my stack I am not getting the actual values from them. From the operators I am receiving the int value for the symbol and for the operands I am getting a box. This is the print function I created for the linked list

1
2
3
4
5
6
7
8
9
10
void print()
{
	if (head == NULL)
		std::cout << "stack is empty\n";
	else
	{
		std::cout << head->data << " ";
		head = head->next;
	}
}


Also, not sure if it might be how I decided to return a value for when I call pop(), but here is that just in case

1
2
3
4
5
6
7
8
DataType pop()
{
	Node* temp = head;
	DataType poppedValue = head->data;
	head = head->next;
	delete temp;
	return poppedValue;
}
Last edited on
Jorge626, nice job! You're almost there.

Stack::print() needs to print the whole stack, not just the top item:
1
2
3
4
5
6
7
    void print (std::ostream &strm)
    {
        for (Node *p = head; p; p = p->next) {
            strm << p->data << "  ";
        }
        strm << '\n';
    }


Lines 16-22: You need to read the whole number, which might be several characters long:
1
2
3
4
5
6
7
            int currentNumber = 0;
            do {
                currentNumber = 10*currentNumber + (int) (infix[i] - '0');
                ++i;
            } while (i<infix.length() && isOperand(infix[i]));
            operands.push(currentNumber);
            --i;                // because you went one too far 


Lines 33 & 34. We push the first operand, then the second. That means when you pop them, you pop the SECOND argument first, and then the first. So you need to reverse lines 33 and 34.
Lines 53-54: same comment
Lines 63-64: same comment
@dhayden
Awesome! Thank you for your help! The calculator part is working now, however I am still running into the problem whenever I enter parenthesis. I keep getting 'head is a NULL pointer' error whenever it encounters a closed parenthesis, I can't figure out what is causing this. Do you have any idea why I am receiving this error?
Do you have any idea why I am receiving this error?
Not without seeing your code :). Please post it.
This is the code that gets the error in my linked list stack class

1
2
3
4
DataType top()
{
	return head->data;
}



This is the block of code that my calculator class calls when I get the error

1
2
3
4
5
6
7
8
9
10
11
else if (infix[i] == ')')
{
	while (operators.top() != '(')
	{
		int valueOne = operands.pop();
		int valueTwo = operands.pop();
		char op = operators.pop();
		operands.push(operate(valueTwo,valueOne,op));
	}
	operators.pop();
}


My pop() and push() functions in Stack class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void push(DataType element)
{
	struct Node* newNode;
	newNode = new Node();
	newNode->data = element;
	newNode->next = head;
	head = newNode;
}

DataType pop()
{
	struct Node* temp;
	temp = head;
	DataType poppedValue = head->data;
	head = head->next;
	temp->next = NULL;
	free(temp);
	return poppedValue;
}


operate function used to push the operand

1
2
3
4
5
6
7
8
9
10
11
12
13
int operate(int valueOne, int valueTwo, char op)
{
	if (op == '+')
		return valueOne + valueTwo;
	if (op == '-')
		return valueOne - valueTwo;
	if (op == '*')
		return valueOne * valueTwo;
	if (op == '/')
		return valueOne / valueTwo;
	if (op == '^')
		return (int)pow(valueOne, valueTwo);
}


Here is my entire stack class just in case it has to do with how I implemented it

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
template <typename DataType>
class Stack {
private:
	struct Node
	{
		DataType data;
		Node* next;
	};
	Node* head;
public:
	Stack() { head = NULL; }

	void push(DataType element)
	{
		struct Node* newNode;
		newNode = new Node();
		newNode->data = element;
		newNode->next = head;
		head = newNode;
	}

	DataType pop()
	{
		struct Node* temp;
		temp = head;
		DataType poppedValue = head->data;
		head = head->next;
		temp->next = NULL;
		free(temp);
		return poppedValue;
	}

	DataType top()
	{
		return head->data;
	}

	bool empty()
	{
		if (head == NULL)
			return true;
		else 
			false;
	}
};
Last edited on
Stack::empty() has a bug.

You should set precedence of (,),[,],{,} to 0, not 4.
@dhayden
I did not catch that bug in empty(), thank you!

Awesome, thank you that took care of the error I was getting! Now that my code is running though, I am getting wrong answers for some equations ): Particularly I noticed in some examples, the last operation pops all my data in my stacks, and when I return the popped function in the end, I just receive the last number in the equation. The examples I am getting the wrong answers for are '17 / ( 2 + 3 ) - 13' and '2 ^ 3 ^ 2' in both cases I am returning the last value , 13 and 2 respectively. With the cout statements you provided to help trace my code, it shows that the operator is being recognized and pushed into the stack, but by the next loop it is popped out and I am left with empty stacks for both my operands and operators. I am not sure where I am losing the value in my operands stack.

Edit: I figured out part of the problem, whenever I would call

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
else if (isOperator(infix[i]) == true)
{
	if (operators.empty() == true || precedence(infix[i]) > precedence(operators.top()))
	{
		std::cout << "Top is called\n";
		operators.push(infix[i]);
	}
	else
	{
		int valueOne = operands.pop();
		int valueTwo = operands.pop();
		char op = operators.pop();
		operands.push(operate(valueOne, valueTwo, op));
	}
}


In my else statement, I would lose the operator currently in infix, I fixed this by just pushing that operator onto the stack after I did the operation in my else statement. This fixed the problem I was having in the equation '17 / ( 2 + 3) - 13' however, I am still getting the wrong answer for '2^3^2, I am not getting the last number as my answer anymore, but I am still getting the wrong answer. I thought it had to do with my precedence since they are the same operation, so I changed the condition in my if statement to being greater than or equal to, but this did not help. Any idea why I am not getting the right answer?

Edit #2: Changing it to greater than or equal to did help. My problem lies in my while loop at the end

1
2
3
4
5
6
7
while (operators.empty() != true)
{
	int valueOne = operands.pop();
	int valueTwo = operands.pop();
	char op = operators.pop();
	operands.push(operate(valueOne, valueTwo, op));
}

In the last iteration, I always end up with an empty operands stack, which I am not sure why I am if the last line of code should be pushing the value after it operates. Do you know why I am ending with an empty stack after this? For now, I just have an inefficient solution. I just made an integer value so I could store the final answer. I set the final answer to the top of the stack after the for loop, and I set it equal to operate after the operands.push(operate()) function in the while loop. I get the correct answers by doing this, but I am sure there is a more efficient way to fix my while loop instead.
Last edited on
Again, please post your whole code. I can't debug your code if I don't have it, and when you post a piece here and a piece there, it's too much trouble to try to reconstruct it. Keep in mind that we're all volunteers here.
Apologies for that, I know you are all volunteers and I really do appreciate all the help you've given me!
Here is my source code:
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
#include "Calculator.h"

int main()
{
std::string userInput, sentinel = "0";
std::cout << "Enter 0 to exit the program.\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

while (userInput != sentinel)	{
	std::cout << "Enter an expression: ";
	std::getline(std::cin, userInput);
	if (userInput == "0")
		break;
	Calculator userExpression(userInput);
	if (userExpression.isLegal(userInput) == false)
		continue;
	else
	{
		std::cout << "\nResult: " << userExpression.eval() << "\n\n";
	}
}

	system("pause");

	return 0;
}


Stack class

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
#pragma once

template <typename DataType>
class Stack {
private:
	struct Node
	{
		DataType data;
		Node* next;
	};
	Node* head;
public:
	Stack() { head = NULL; }

	void push(DataType element)
	{
		struct Node* newNode;
		newNode = new Node();
		newNode->data = element;
		newNode->next = head;
		head = newNode;
	}

	DataType pop()
	{
		struct Node* temp;
		temp = head;
		DataType poppedValue = head->data;
		head = head->next;
		temp->next = NULL;
		free(temp);
		return poppedValue;
	}

	DataType top()
	{
		return head->data;
	}

	bool empty()
	{
		if (head == NULL)
			return true;
		else 
			return false;
	}
};


Calcultor class
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#pragma once
#include <string>
#include <iostream>
#include <math.h>
#include "Stack.h"


class Calculator
{
private:
	std::string infix;
	Stack <char> operands;
	Stack <int> operators;

public: 
	Calculator() { std::string infix = ""; }
	Calculator(std::string userExpr) { infix = userExpr; }

	bool isLegal(std::string infix)
	{
		bool legal = true;
		int parenthesisCount = 0;
		try 
		{
			for (size_t i = 0; i < infix.length(); i++)
			{
				switch (infix[i])
				{
				case '(': parenthesisCount++; break;
				case ')': parenthesisCount--; break;
				default: break;
				}
			}
			if (parenthesisCount != 0)
			{
				legal = false;
				throw 0;
			}
		}
		catch (...)
		{
			std::cout << "Error: Unmatched Parenthesis!\n\n";
		}
		try 
		{
			for (size_t i = 0; i < infix.length(); i++)
			{
				switch (infix[i])
				{
				case '.': legal = false; throw 1; break;
				default: break;
				}
			}
		}
		catch (...)
		{
			std::cout << "Error: Operands must be integers!\n\n";
		}
		return legal;
	}

	bool isOperand(char number)
	{
		if (number >= '0' && number <= '9')
			return true;
		else
			return false;
	}

	bool isOperator(char op)
	{
		if (op == '+' || op == '-' || op == '*' || op == '/' || op == '(' || op == ')' ||op == '[' || op == ']' || op == '{' || op == '}' || op == '^')
			return true;
		else
			return false;
	}

	bool isSpace(char character)
	{
		if (character == ' ' || character == '\r' || character == '\n')
			return true;
		else
			return false;
	}

	int precedence(char character)
	{
		if (character == '^')
			return 3;
		else if (character == '*' || character == '/')
			return 2;
		else if (character == '+' || character == '-')
			return 1;
		else if (character == '(' || character == ')' || character == '[' || character == ']' || character == '{' || character == '}')
			return 0;
		else 
			return -1;
	}

	int operate(int valueOne, int valueTwo, char op)
	{
		switch (op) 
		{
		case '+': return valueOne + valueTwo; break;
		case '-': return valueOne - valueTwo; break;
		case '*': return valueOne * valueTwo; break;
		case '/': return valueOne / valueTwo; break;
		case '^': return (int)pow(valueOne, valueTwo); break;
		}
	}

	int eval()
	{
		int finalAnswer;
		for (size_t i = 0; i < infix.length(); i++)
		{
			if (isOperand(infix[i]) == true)
			{
				int currentNumber = 0;
				do 
				{
					currentNumber = 10 * currentNumber + (int)(infix[i] - '0');
					++i;
				} while (i < infix.length() && isOperand(infix[i]));
				operands.push(currentNumber);
				--i;
			}

			else if (infix[i] == '(')
				operators.push(infix[i]);
			
			else if (infix[i] == ')')
			{
				while (operators.top() != '(')
				{
					int valueOne = operands.pop();
					int valueTwo = operands.pop();
					char op = operators.pop();
					operands.push(operate(valueTwo,valueOne,op));
				}
				operators.pop();
			}

			else if (isSpace(infix[i]) == true)
				continue;

			else if (isOperator(infix[i]) == true)
			{
				if (operators.empty() == true || precedence(infix[i]) >= precedence(operators.top()))
				{
					operators.push(infix[i]);
				}
				else
				{
					int valueTwo = operands.pop();
					int valueOne = operands.pop();
					char op = operators.pop();
					operands.push(operate(valueOne, valueTwo, op));
					operators.push(infix[i]);
				}
			}
		}
		finalAnswer = operands.top();

		while (operators.empty() != true)
		{
			int valueTwo = operands.pop();
			int valueOne = operands.pop();
			char op = operators.pop();
			operands.push(operate(valueOne, valueTwo, op));
			finalAnswer = operate(valueOne, valueTwo, op);
		}

		return finalAnswer;
	}
	
	void setExpr(std::string infixExpr)
	{
		infix = infixExpr;
	}

	std::string getExpr()
	{
		return infix;
	}
};
so I changed the condition in my if statement to being greater than or equal to, but this did not help
By making it >=, you effectively changed it so that equal precedence operators are evaluated right-to-left instead of left-to-right. So change it back. When I make that change, 2^3^2 returns 64, which seems right, at least for this assignment.
You're right, I asked my professor about it and the equation he meant to put was 2 ^ ( 3 ^ 2 ), which gives me the answer I was looking for. Thank you for your help! I think that fixes everything
Topic archived. No new replies allowed.