Error reading/matching stack

I have a program that is supposed to match opening and closing braces. It's supposed to work like a stack. It works until the last line of the input file. The last line is matching, but because it thinks the array size is zero it drops out and states that it doesn't match. I could really use some help on this. I have been looking at this and just can't see it. Any help, tips, comments, or advice is greatly apppreciated!


( ) 
Parenthesis matched
[ ] ( ) { } 
Parenthesis matched
[ { ( ) } ] 
Parenthesis matched
[ [ ) ) 
Parenthesis Not matched
{ ( ) [ ( ) ] 
Parenthesis Not matched


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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
class List
{
      public:
        //Typedef declarations
        typedef char ET;//Element Type
        static const ET CAPACITY = 20; //Size of the array

        List()
        {
            //Set to zero
            Position = 0;
            Used = 0;

            //Zero out array
            for(int i = 0; i < CAPACITY; i++)
            {
                MyAry[i] = 0;
            }
        }

		 void front()
        {
            //Sets position to the first location
            Position = 0;
        }

        void end()
        {
            //Gets the end position
            if(Used != 0)
            {
                Position = Used - 1;
            }
        }

        void prev()
        {
            //Subtracts one from current position
            if(Position - 1 >= 0)
            {
                Position--;
            }
        }


        bool empty()
        {
            //Used = 0 is returns true
            return Used;
        }

        void next()
        {
            //Adds one to current position
            if(Position + 1 < Used)
            {
                Position++;
            }
        }

        int getPos()
        {
            //Returns current Position
            return Position + 1;
        }


        void setPos(int NewPos)
        {
            //Sets Position to New Position
            if((NewPos >= 0) && (NewPos < Used))
            {
                Position = NewPos;
            }
        }

        bool insertBefore(ET value)
        {
            //Checks for position out of bounds
            if(Position + 1 > CAPACITY)
            {
                return false;
            }
            else
            {
                //If used is zero then first element
                if(Used == 0)
                {
                    MyAry[Used] = value;
                }
                else
                {
                    //Shuffles everything down to make room for new element
                    for(int i = Used; i > Position; i--)
                    {
                        MyAry[i] = MyAry[i - 1];
                    }
                    MyAry[Position] = value;
                }

                Used++;

                return true;
            }
        }

	ET getElement()
        {
            return MyAry[Position];
        }

        bool insertAfter(ET value)
        {
            //Checks for position out of bounds
            if(Position + 1 > CAPACITY)
            {
                return false;
            }
            else
            {
                //If used is zero then first element
                if(Used == 0)
                {
                    MyAry[Used] = value;
                }
                else
                {
                    //Moves everything down to make room for new element
                    for(int i = Used; i > Position + 1; i--)
                    {
                        MyAry[i] = MyAry[i - 1];
                    }
                    MyAry[Position] = value;
                }

                //Increment
                Position++;
                Used++;

                return true;
            }
        }

        int size()
        {
            //Returns size of the array
            return Used;
        }

        void replace(int RepVal)
        {
            //Replace current value at position with new value
            MyAry[Position] = RepVal;
        }

        void clear()
        {
            //Return the List back to zero
            for(int i = 0; i < CAPACITY; i++)
            {
                MyAry[i] = 0;
            }

            //Return values to zero
            Position = 0;
            Used = 0;
        }

        void erase()
        {
            //Move all remaining elements down
            for(int i = Position; i < Used; i++)
            {
                MyAry[i] = MyAry[i + 1];
            }

            //Decrement Used
            Used--;
        }
             friend ostream &operator<<(ostream &stream,List L);

    private:
        ET MyAry[CAPACITY];
        int Used;
        int Position;

};

ostream &operator<<(ostream &stream,List L)
{
       for(int i=0;i<L.Used;i++)
       {

             stream<<L.MyAry[i]<<" ";
       }
       stream<<endl;
       return stream;
}

//Declare file streams
ifstream inFile;
ofstream outFile;

int main()
  {
  	List stack;
	char inputArray[8];
	char value;
	int i=0;
	bool flag=true;

    //Open inFile
    inFile.open("StackStr.txt");

    while(!inFile)
    {
        cout << "Error opening the inFile." << endl;
        return 1;
    }

    //Open outFile
    outFile.open("StackResults.txt");

    while(!outFile)
    {
        cout << "Error opening the outFile." << endl;
        return 1;
    }

	while(inFile)
	{
		inFile.getline(inputArray, 8,'\n');
		i = 0;
		flag = true;

		while(inputArray[i] != '\0')
		{
			value=inputArray[i];
			cout << value << " ";
			outFile << value << " ";
			i++;

			if(value=='(' || value=='[' || value=='{')
				stack.insertBefore(value);
            else if(value==')' || value==']' || value=='}')
			{
				if(stack.size()==0)
				{
					flag=false;
					break;
				}
				else
				{
					stack.front();
					if((stack.getElement()=='(' && value==')'))
					{
						flag=true;
						stack.erase();
					}
					else if((stack.getElement()=='[' && value==']'))
					{
						flag=true;
						stack.erase();
					}
					else if((stack.getElement()=='{' && value=='}'))
					{
						flag=true;
						stack.erase();
					}
					else
					{
						flag=false;
						cout << value << " ";
                        outFile << value << " ";
						break;
					}
				}
			}
		}
		if(stack.size() == 0)
		{
            flag = true;
		}
		else
		{
            flag = false;
		}


		if(flag == true)
		{
			cout<<endl<<"Parenthesis matched" << endl;
			outFile<<endl<<"Parenthesis matched" << endl;
		}
		else
		{
			cout<<endl<<"Parenthesis Not matched" << endl;
			outFile<<endl<<"Parenthesis Not matched" << endl;
		}
	}
	system("pause");
	return 0;
  }
1
2
3
4
while( token=get_token(input) )
   if( not parenthesis_insert(stack, token) )
      return false;
return true;


Apart:
_ if(flag == true) ¿why do you do that?
_ typedef char ET;//Element Type you better typedef char Element_Type;
_
1
2
stack.front();
stack.getElement();
is error prone.
_ {push,pop}back() are more efficient than {push,pop}front() in arrays.
_ A stack that is actually a list, that is actually an array.
Thank you for your suggestions. I did modify my typedef to reflect your suggestion. Unfortunately, I do not understand what

while( token=get_token(input) )
if( not parenthesis_insert(stack, token) )
return false;
return true;


means. I know this is a crazy assignment. Our instructor told us to use the List class we created in a previous project and we aren't allowed to used the builts that C++ has to offer. I do appreciate any help that can be provided.
I suggest you to simplify your analysis.
In your code `flag' is true and you are setting it to true again. It makes it hard to follow.
Like when you do
1
2
3
4
5
    while(!inFile)
    {
        cout << "Error opening the inFile." << endl;
        return 1;
    }
that is not a while but an if

So, worry only for the operations that could go wrong, the insertion of a closing non-matching parenthesis.
1
2
3
4
5
6
7
8
bool parenthesis_match( const char *input ){
   char token;
   List stack;
   while( token=get_token(input) )
      if( not parenthesis_insert(stack, token) )
         return false;
   return is_empty(stack);
}
I have changed my code, hopefully for the better, but now it's saying that nothing matches. It's showing that nothing matches because the size of the array never appears as zero. I'm sure I'm overlooking something stupid. Any help is greatly appreciated!

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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
class List
{
      public:
        //Typedef declarations
        typedef char Element_Type;//Element Type
        static const Element_Type CAPACITY = 20; //Size of the array

        List()
        {
            //Set to zero
            Position = 0;
            Used = 0;

            //Zero out array
            for(int i = 0; i < CAPACITY; i++)
            {
                MyAry[i] = 0;
            }
        }

		 void front()
        {
            //Sets position to the first location
            Position = 0;
        }

        void end()
        {
            //Gets the end position
            if(Used != 0)
            {
                Position = Used - 1;
            }
        }

        void prev()
        {
            //Subtracts one from current position
            if(Position - 1 >= 0)
            {
                Position--;
            }
        }

        bool empty()
        {
            //Used = 0 is returns true
            return Used;
        }

        void next()
        {
            //Adds one to current position
            if(Position + 1 < Used)
            {
                Position++;
            }
        }

        int getPos()
        {
            //Returns current Position
            return Position + 1;
        }


        void setPos(int NewPos)
        {
            //Sets Position to New Position
            if((NewPos >= 0) && (NewPos < Used))
            {
                Position = NewPos;
            }
        }

        bool insertBefore(Element_Type value)
        {
            //Checks for position out of bounds
            if(Position + 1 > CAPACITY)
            {
                return false;
            }
            else
            {
                //If used is zero then first element
                if(Used == 0)
                {
                    MyAry[Used] = value;
                }
                else
                {
                    //Shuffles everything down to make room for new element
                    for(int i = Used; i > Position; i--)
                    {
                        MyAry[i] = MyAry[i - 1];
                    }
                    MyAry[Position] = value;
                }

                Used++;

                return true;
            }
        }

		Element_Type getElement()
        {
            return MyAry[Position];
        }

        bool insertAfter(Element_Type value)
        {
            //Checks for position out of bounds
            if(Position + 1 > CAPACITY)
            {
                return false;
            }
            else
            {
                //If used is zero then first element
                if(Used == 0)
                {
                    MyAry[Used] = value;
                }
                else
                {
                    //Moves everything down to make room for new element
                    for(int i = Used; i > Position + 1; i--)
                    {
                        MyAry[i] = MyAry[i - 1];
                    }
                    MyAry[Position] = value;
                }

                //Increment
                Position++;
                Used++;

                return true;
            }
        }

        int size()
        {
            //Returns size of the array
            return Used;
        }

        void replace(int RepVal)
        {
            //Replace current value at position with new value
            MyAry[Position] = RepVal;
        }

        void clear()
        {
            //Return the List back to zero
            for(int i = 0; i < CAPACITY; i++)
            {
                MyAry[i] = 0;
            }

            //Return values to zero
            Position = 0;
            Used = 0;
        }

        void erase()
        {
            //Move all remaining elements down
            for(int i = Position; i < Used; i++)
            {
                MyAry[i] = MyAry[i + 1];
            }

            //Decrement Used
            Used--;
        }
             friend ostream &operator<<(ostream &stream,List L);

    private:
        Element_Type MyAry[CAPACITY];
        int Used;
        int Position;

};

class Stack
{
    public:
        typedef char Element_Type;//Element Type

        void push(Element_Type value)
        {
            Stack::myStack.end();
            myStack.insertAfter(value);
        }

        void pop()
        {
            myStack.end();
            myStack.erase();
        }

        int size()
        {
            return myStack.size();
        }

        Element_Type top()
        {
            Stack::myStack.end();
            myStack.getElement();

            return myStack.getElement();
        }

        bool empty()
        {
            return myStack.empty();
        }

        void clear()
        {
            myStack.clear();
        }


    private:
        List myStack;
};

ostream &operator<<(ostream &stream,List L)
{
       for(int i=0;i<L.Used;i++)
       {

             stream<<L.MyAry[i]<<" ";
       }
       stream<<endl;
       return stream;
}

//Declare file streams
ifstream inFile;
ofstream outFile;

int main()
  {
  	Stack stack1;
	char inputArray[20];
	char value;
	int i=0;

    //Open inFile
    inFile.open("StackStr.txt");

    while(!inFile)
    {
        cout << "Error opening the inFile." << endl;
        return 1;
    }

    //Open outFile
    outFile.open("StackResults.txt");

    while(!outFile)
    {
        cout << "Error opening the outFile." << endl;
        return 1;
    }

	while(inFile)
	{
        for(int j = 0; i < 20; i++)
        {
            inputArray[j] = ' ';
        }
        stack1.clear();

		inFile.getline(inputArray, 20,'\n');
		i = 0;

		while(inputArray[i] != '\0')
		{
			value=inputArray[i];
			cout << value << " ";
			outFile << value << " ";
			i++;

			if(value=='(' || value=='[' || value=='{')
            {
				stack1.push(value);
            }
            else if(value==')' || value==']' || value=='}')
			{
			    if(stack1.top() == value)
			    {
                    stack1.pop();
			    }
			}
		}

		if(stack1.empty() == 0)
		{
            cout<<endl<<"Parenthesis matched" << endl << endl;
			outFile<<endl<<"Parenthesis matched" << endl << endl;
		}
		else
		{
            cout<<endl<<"Parenthesis Not matched" << endl << endl;
			outFile<<endl<<"Parenthesis Not matched" << endl << endl;
		}
	}

	system("pause");
	return 0;
  }
Please indent your code.
1
2
3
else if(value==')' || value==']' || value=='}')
	if(stack1.top() == value)
		stack1.pop();
is clearly wrong. You need to find the matching pair, no the same character.

1
2
3
4
5
        bool empty()
        {
            //Used = 0 is returns true
            return Used;
        }
is counter intuitive. (or plain wrong)
Sorry about the code. I'm just really frustrated. I have changed my code again, but I'm still returning the same values.

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
	while(inFile)
	{
        for(int j = 0; i < 20; i++)
        {
            inputArray[j] = ' ';
        }
        stack1.clear();

		inFile.getline(inputArray, 20,'\n');
		i = 0;

		while(inputArray[i] != '\0')
		{
			value=inputArray[i];
			//cout << "size: " << stack1.size() << endl;
			cout << value << " ";
			outFile << value << " ";
			i++;

			if(value=='(' || value=='[' || value=='{')
            {
				stack1.push(value);
            }
            else if(value==')' || value==']' || value=='}')
			{
			    if(stack1.top() == '(' && value == ')')
			    {
                    stack1.pop();
			    }
			    else if(stack1.top() == '[' && value == ']')
			    {
                    stack1.pop();
			    }
                else if(stack1.top() == '{' && value == '}')
			    {
                    stack1.pop();
			    }
			}
		}

		if(stack1.size() == 0)
		{
            cout<<endl<<"Parenthesis matched" << endl << endl;
			outFile<<endl<<"Parenthesis matched" << endl << endl;
		}
		else
		{
            cout<<endl<<"Parenthesis Not matched" << endl << endl;
			outFile<<endl<<"Parenthesis Not matched" << endl << endl;
		}
	}
Topic archived. No new replies allowed.