Problem with stack

I am writing a program that will convert an expression from infix notation to postfix notation for a class project. My program compiles fine but when it tries the conversion, it sometimes stops working and sometimes it just continues to run without doing anything. Here is my entire 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
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
#include <iostream>
#include <string>
using namespace std;

struct node{
	char data;
	node* next;
};
struct head{
	int count;
	node* top;
};
class stack{
private:
	head h;
public:
	stack();			//default constructor
	~stack();			//destructor

	void push(char);	//adds a character to the top of the stack
	char pop();			//removes a character from the top of the stack, and returns the character value or NULL if stack is empty
	char stackTop();	//returns the character at the top of the stack without removing it
	bool isEmpty();		//returns true if stack is empty
	
};
stack::stack(){
	h.count = 0;
	h.top = NULL;
}
stack::~stack(){
	node *current = h.top, *previous;
	while(current->next != NULL){
		previous = current;
		current = current->next;
		delete previous;
	}
	delete current;
}
void stack::push(char c){
	node* newnode = new node;
	if(h.top == NULL){
		newnode->data = c;
		newnode->next = NULL;
		h.top = newnode;
		h.count = 1;
	}
	else{
		newnode->data = c;
		newnode->next = h.top;
		h.top = newnode;
		h.count++;
	}
}
char stack::pop(){
	if(isEmpty())
		return NULL;
	char c;
	c = h.top->data;
	if(h.top->next == NULL)
		h.top = NULL;
	else
		h.top = h.top->next;
	h.count--;
	return c;
}
char stack::stackTop(){
	if(h.top == NULL)
		return NULL;
	return h.top->data;
}
bool stack::isEmpty(){
	if(h.top == NULL)
		return true;
	return false;
}
class expression{
private:
	string infix;
	string postfix;
public:
	expression();			//default constructor

	void setInfix();		//changes the infix expression
	string getInfix();		//returns infix expression
	void updatePostfix();	//updates postfix expression based on infix expression
	string getPostfix();	//returns postrix expression
};
expression::expression(){
	infix = "";
	postfix = "";
}
void expression::setInfix(){
	cout << "Type in your infix expression:" << endl;
	cin >> infix;
}
string expression::getInfix(){
	return infix;
}
void expression::updatePostfix(){
	stack s;
	string str = "";
	for(int i = 0; i < infix.length(); i++){		//go through each character
		char c = infix.at(i);
		if(c == '(')								//add ( to the stack
			s.push(c);
		else if(c == ')'){
			while(s.stackTop() != '(');				//extract characters from the stack until a ( is met
				str += s.pop();
			s.pop();								//get rid of the (
		}
		else if(c == '*' || c == '/')				//has the highest priority, so push it to the stack
			s.push(c);
		else if(c == '+' || c == '-'){				//has a lower priority
			if(s.stackTop() == '*' || s.stackTop() == '/'){//check if the top of the stack has a higher priority
				while(s.stackTop() != '(' && !s.isEmpty())//if so, extract characters until the stack is empty or a ( is met
					str += s.pop();
				s.pop();							//will do nothing if stack is empty, will get rid of the ( if not
			}
			else									//top of stack is not a higher priority, so push the + or - to the stack
				s.push(c);
		}
		else										//add the operand to the final string
			str += c;
	}
	while(!s.isEmpty())		//finish extracting operators
		str += s.pop();
	postfix = str;
}
string expression::getPostfix(){
	return postfix;
}
int main(){
	int select;
	expression e;
	do{
		system("CLS");
		cout << "Please select one of the following:" << endl;
		cout << "1. Change infix expression" << endl;
		cout << "2. Show infix expression" << endl;
		cout << "3. Update postfix expression" << endl;
		cout << "4. Show postfix expression" << endl;
		cout << "5. Quit" << endl;
		cin >> select;
		system("CLS");
		switch(select){
			case 1:
				e.setInfix();
				break;
			case 2:
				cout << e.getInfix() << endl;
				system("PAUSE");
				break;
			case 3:
				e.updatePostfix();
				break;
			case 4:
				cout << e.getPostfix() << endl;
				system("PAUSE");
		}
	}while(select < 5);

	return 0;
}

Please help, I can't seem to find the problem and im using microsoft visual c++ express edition, and there is no debugger.
Last edited on
No debugger? Yes there is. MSVSE 2005 and 2008 both have debuggers from every version I've seen.
Are you sure you can return NULL as char value?
Well the debugger won't work on mine, it always gives a pop up saying there is no debug information or that it is not valid, and then it will not show the autos, etc.
I'm pretty sure I can return NULL as a character value. I thought that was the problem at first and I changed it to '0' to see if that fixed it, but it didn't so I changed it back.
Last edited on
Would '\0' make any difference. As NULL, 0 and '\0' got me last time :)
That didn't work, either.
You need to compile for Debug rather than Release if you want the debugger to show your symbols.
I thought that was what I was doing... I go up to the debug menu and click "Start Debugging" and it says project out of date blah blah would you like to build so I say yes. Then when it's finished compiling, it gives me that debug error popup thing.
That's something else. In Visual Studio they're called Configurations, you get two by default, Debug and Release.

The Debug Configuration is set up to generate the information the debugger needs. The Release Configuration is set up to create a production binary.

You can run the debugger with any executable, but if it doesn't have the expected kinds of symbols, you won't see the details of your code.

You're expected to develop your code using the Debug Configuration, you're not expected to release code built this way. When you're happy with your code, you're expected to switch to the Release Configuration and work with that.

I suspect you're building in Release. You should check that you're compiling using the Debug Config.
Ok, I'll try that.

EDIT:
It didn't work, it's in debug mode and here's the error it gives me:
"Debugging information for 'Postfix.exe' cannot be found or does not match. Binary was not built with debug information. Do you want to continue debugging?"
Last edited on
Topic archived. No new replies allowed.