Stack Code Help.

Having some issues with figuring out syntax.

The assignment:

Stack Homework:

Write a stack program that can determine whether or not the following symbols are balanced: ( ) { } [ ] /* */

If a left symbol is found, push it onto the stack. If a right symbol is found top/pop the stack.

Test your code well.

There should be no prompt for an output file, no long scroll of tokens or message's. Hard code the input filename as: stack.cpp

Example output: No errors found.
or
Error, symbol not balanced: {

** Note: If a symbol is found that does not match and pair with last push, end the program.

Maybe some of you can give me a hand in getting this syntax right.


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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>

using namespace std;

/*----------------------------------------------------------------------------------------------------*/

class stack
{
	struct stackListNode	// Struct linked list
		{
			int data;
			stackListNode *next;
		};

	stackListNode *top;

public:

		stack()	// Creates stack, and sets top to NULL
			{
				top = NULL;
			}


/*----------------------------------------- Push ------------------------------------------------------*/

		void push(int token)	// Push token onto the stack and create new node for top of stack
			{
				stackListNode *newNode = new stackListNode;
				newNode-> data = token;
				newNode-> next = top;
				top = newNode;
			}

/*--------------------------------------- Pop ---------------------------------------------------------*/

		int pop()
			{
				if(IsEmpty())
				{
					cout << "Stack is empty!\n" << endl;
					return NULL;				
				}

				stackListNode *temp = top;
				top = temp-> next;
			}

/*------------------------------------------ Peek ---------------------------------------------------------*/
			
		int peek()
			{
				if(IsEmpty())
				{
					cout << "Stack is empty!\n" << endl;
					return NULL;				
				}

				return top-> data;
			}

/*--------------------------------------------- Is Empty ---------------------------------------------------*/

		int IsEmpty()
			{
				return top == NULL;
			}
}	
/*---------------------------------------------------------------------------------------------------------*/		


/*--------------------------------------------- Main ------------------------------------------------------*/

int main(int argc, char *argv[])
{

if(argc != 2)	// Usage statement of how to use program via command line
	{
		cout << "\nUsage: " << argv[0] << " 'stack.cpp'\n" <<  endl;
		return(1);
	}

ifstream input_File;
input_File.open("stack.cpp", ios::in);

if(!input_File.is_open())
	{	
		cout << "Error: Unable to open input file\n" << endl;
		return(1);
	}

/*-------------------------------------------- Push Switch/Case -------------------------------------------------*/
char symbol;
char openComment = '/*';
char closeComment = '*/';

while(input_File)
	{
		input_File.get(symbol);
			if(symbol == ' ') continue;	// If character is 'space', continue
				
	else
	{
		switch(symbol)
			{
				case '(': stack::push;	// Push (
					break;

				case '{': stack::push;	// Push {
					break;

				case '[': stack::push;	// Push [
					break;

				case '/':				// Read /, continue, read next: if = *, then symbol value = */, push symbol
					continue;
						if(symbol = '*')
							{
								symbol = openComment;
								stack::push;
							}
					break;
/*------------------------------------------ Pop Switch/Case ------------------------------------------------------*/
				case '*': 
					continue;
						if(symbol = '/')
							{
								symbol = closeComment;
								stack::pop;
							}
					break;

				case ')': 
					if(? = '(')   // Error: I cannot figure out the syntax to read the top of the stack
								  //        and compare it with that symbol
						{
							stack::pop;
						}
					else
						{
							cout << "Error! Symbol not balanced: " << symbol << endl;
							return (1);
						}
					break;

				case '}': 
					if(? = '{')
						{
							stack::pop;
						}
					else
						{
							cout << "Error! Symbol not balanced: " << symbol << endl;
							return (1);
						}	
					break;

				case ']': 
					if(? = '[')
						{
							stack::pop;
						}
					else
						{
							cout<< "Error! Symbol not balanced: " << symbol << endl;
							return (1);
						}
					break; 

			}
	}

	return 0; // Success
}


Errors are marked in the code itself.
Last edited on
stack::push and stack::pop are wrong way to execute methods (not mentrioning missing parameters) in your program.

If the stack is defined as pointer to dynamic object then you write stack->pop(); and when it is static stack.pop(); .

You also didnt initialize the stack. Class stack that you created is just a definition of object (type). Same as int, char or float you must create it:
stack myStack;
then you can execure methods:
mystack.push();
etc.
Last edited on
Ok, I've changed my code to this:

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>

using namespace std;

/*----------------------------------------------------------------------------------------------------*/

class stack
{
   struct stackListNode	// Struct linked list
	  {
		char data;
		stackListNode *next;
	  };

	  stackListNode *top;

public:

   void push(char);
   int pop();
   int peek();
   int IsEmpty();

	   stack()	// Creates stack, and sets top to NULL
		{
		  top = 0;
		}
};


/*----------------------------------------- Push ------------------------------------------------------*/

	void stack::push(char symbol)	// Push symbol onto the stack and create new node for top of stack
		   {
			  stackListNode *newNode = new stackListNode;
			  newNode-> data = symbol;
			  newNode-> next = top;
			  top = newNode;
		   }

/*--------------------------------------- Pop ---------------------------------------------------------*/

	int stack::pop()
		   {
			  if(IsEmpty())
				{
				   cout << "Stack is empty!\n" << endl;
				   return (2);				
				}

			stackListNode *temp = top;
			top = temp-> next;
		   }

/*------------------------------------------ Peek --------------------------------------------------------*/
			
	int stack::peek()
		{
		   if(IsEmpty())
			{
			   cout << "Stack is empty!\n" << endl;
			   return (2);				
			}

		  return top-> data;
		}

/*--------------------------------------------- Is Empty --------------------------------------------------*/

	int stack::IsEmpty()
	   {
		  return top == NULL;
	   }

/*--------------------------------------------- Main ------------------------------------------------------*/

int main(int argc, char *argv[])
{

ifstream input_File;
input_File.open ("stack.cpp");		// "stack.cpp"

if(!input_File.is_open())
   {	
	  cout << "Error: Unable to open input file\n" << endl;
	  return(1);
   }

/*-------------------------------------------- Push Switch/Case-----------------------------------------*/
stack myStack;
char symbol;
char commentSlash = '/';
char commentStar = '*';

if(input_File.is_open())
 {
  while(!input_File.eof())
    {
	  input_File.get(symbol);
		if(symbol == ' ')	// If character is 'space', continue
		   continue;

	else
	{
	   switch(symbol)
		  {
			case '(': myStack.push(symbol);	// Push ( onto stack
				cout << "Top of Stack: " << myStack.peek() << endl << endl;

			case '{': myStack.push(symbol);	// Push { onto stack
				cout << "Top of Stack: " << myStack.peek() << endl << endl;

			case '[': myStack.push(symbol);	// Push [ onto stack
				cout << "Top of Stack: " << myStack.peek() << endl << endl;

			case '/':						// Read /, continue, read next: if = *, 
			   input_File.get(symbol);		// then symbol value = */, push symbol
			      if(symbol == '*')
					{
					   symbol = commentSlash;	// Push /
					   myStack.push(symbol);
					   cout << "Top of Stack: " << myStack.peek() << endl << endl;

					   symbol = commentStar;	// Push *
					   myStack.push(symbol);
					   cout << "Top of Stack: " << myStack.peek() << endl << endl;
					}
					
/*------------------------------------------ Pop Switch/Case -------------------------------------------*/
		    case '*': 
			   input_File.get(symbol);
				  if(symbol == '/')
					{
					   myStack.pop();		// Pop *
					   myStack.pop();		// Pop /
					}

			case ')': 
			   if(myStack.peek() == '(')   
				 {
				   myStack.pop();	// Pop match ( )
				 }
				   else
					{
					   cout << "Error! Symbol not balanced: " << symbol << endl;
					   return (1);
					}

			case '}':				
			   if(myStack.peek() == '{')
				 {
				   myStack.pop();	// Pop match { }
				 }
				   else
					{
					  cout << "Error! Symbol not balanced: " << symbol << endl;
					  return (1);
					}	

			case ']': 
			   if(myStack.peek() == '[')				
				 {
				   myStack.pop();	// Pop match [ ]
				 }
				   else
					{
					   cout<< "Error! Symbol not balanced: " << symbol << endl;
					   return (1);
					}
					break; 

			} // end switch
	  } // end else
	} // end while
  }// end if
		
		cout << "No Errors Found." << endl << endl;

 return 0; // Success
}// end main 

------------------------------------------------------------------------------------------------------------------
I believe I am not pushing the symbols onto the stack correctly, as well as popping...
Any suggestions?
Last edited on
your stack implementation is not working. Also, pop() is not returning a value. There are also missing delete keywords.

Read this page (or use the code) http://en.wikipedia.org/wiki/Stack_(abstract_data_type)
Last edited on
I have updated my code again above.

I am getting an output of this:


C:\Users\Zero>a.exe
Top of Stack: 40

Top of Stack: 40

Top of Stack: 40

Error! Symbol not balanced: (

Any suggestions??
Figured it out. Needed to put in breaks.
break;

Also, changed int stack::peek() to char stack::peek().

See final (working) code below, if interested:

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
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>

using namespace std;

/*----------------------------------------------------------------------------------------------------*/

class stack
{
   struct stackListNode	// Struct linked list
	  {
		char data;
		stackListNode *next;
	  };

	  stackListNode *top;

public:

   void push(char);
   int pop();
   char peek();
   int IsEmpty();

	   stack()	// Creates stack, and sets top to NULL
		{
		  top = 0;
		}
};


/*----------------------------------------- Push ------------------------------------------------------*/

	void stack::push(char symbol)	// Push symbol onto the stack and create new node for top of stack
		   {
			  stackListNode *newNode = new stackListNode;
			  newNode-> data = symbol;
			  newNode-> next = top;
			  top = newNode;
		   }

/*--------------------------------------- Pop ---------------------------------------------------------*/

	int stack::pop() // Pop function
		   {
			  if(IsEmpty())
				{
				   cout << "Stack is empty!\n" << endl;
				   return (2);				
				}

			stackListNode *temp = top;
			top = temp-> next;
		   }

/*------------------------------------------ Peek --------------------------------------------------------*/
			
	char stack::peek() // Peek function
		{
		   if(IsEmpty())
			{
			   cout << "Stack is empty!\n" << endl;
			   return (2);				
			}

		  return top-> data;
		}

/*--------------------------------------------- Is Empty --------------------------------------------------*/

	int stack::IsEmpty() // Empty stack function
	   {
		  return top == NULL;
	   }

/*--------------------------------------------- Main ------------------------------------------------------*/

int main(int argc, char *argv[])
{

ifstream input_File;
input_File.open ("stack.cpp");		// "stack.cpp"

if(!input_File.is_open())
   {	
	  cout << "Error: Unable to open input file\n" << endl;
	  return(1);
   }

/*-------------------------------------------- Push Switch/Case -------------------------------------------------*/
stack myStack;
char symbol;
char commentSlash = '/';
char commentStar = '*';

if(input_File.is_open())
 {
  while(!input_File.eof())
    {
	  input_File.get(symbol);
		if(symbol == ' ')	// If character is 'space', continue
		   continue;

	else
	{
	   switch(symbol)
		  {
			case '(': myStack.push(symbol);	// Push ( onto stack
				break;

			case '{': myStack.push(symbol);	// Push { onto stack
				break;

			case '[': myStack.push(symbol);	// Push [ onto stack
				break;

			case '/':						// Read /, continue, read next: if = *, 
			   input_File.get(symbol);		// then symbol value = */, push symbol
			      if(symbol == '*')
					{
					   symbol = commentSlash;	// Push /
					   myStack.push(symbol);
					  
					   symbol = commentStar;	// Push *
					   myStack.push(symbol);
					}
				  break;
					
/*------------------------------------------ Pop Switch/Case ------------------------------------------------------*/

		    case '*': 
			   input_File.get(symbol);
				  if(symbol == '/')
					{
					   myStack.pop();		// Pop *
					   myStack.pop();		// Pop /
					}
				       else
						{
						   cout << "\nError! Symbol not balanced: " << symbol << endl;
					       cout << "Exiting..." << endl;
					       return (1);
						}
				  break;

			case ')': 
			   if(myStack.peek() == '(')   
				 {
				   myStack.pop();	// Pop match ( )
				 }
				   else
					{
					   cout << "\nError! Symbol not balanced: " << symbol << endl;
					   cout << "Exiting..." << endl;
					   return (1);
					}
			   break;

			case '}':				
			   if(myStack.peek() == '{')
				 {
				   myStack.pop();	// Pop match { }
				 }
				   else
					{
					  cout << "\nError! Symbol not balanced: " << symbol << endl;
					  cout << "Exiting..." << endl;
					  return (1);
					}
			   break;

			case ']': 
			   if(myStack.peek() == '[')				
				 {
				   myStack.pop();	// Pop match [ ]
				 }
				   else
					{
					   cout<< "\nError! Symbol not balanced: " << symbol << endl;
					   cout<< "Exiting..." << endl;
					   return (1);
					}
					break; 

			} // end switch
	  } // end else
	} // end while
  } // end if
		
		cout << "\nNo Errors Were Found." << endl << endl;

 return 0; // Success
} // end main 


Thanks for all of your help!
Topic archived. No new replies allowed.