Weird error

I get a weird error where I get a weird symbol that looks like this "╠" can anyone help me on what is going on? Everything works correctly except the last line. I only included my function but if I need to include more of my program I will. Thanks in advance for any help.

Input file:

()
[](){}
[{()}]
<><<<>>>
[[))
{()[()]

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
  void readFile(stack &stck)
{
	fstream infile;
	char temp = 'a';
	infile.open("first.txt");

	temp = infile.get();

	while (temp != '\n')
	{
	cout << "Input string:" << endl;
	
	while (temp != '\n')
	{

		if (temp == '(' || temp == ')' || temp == '[' || temp == ']' || temp == '{' || temp == '}' || temp == '<' || temp == '>')
		{
			if (temp == '(' || /*temp == ')' ||*/ temp == '[' || /*temp == ']' ||*/ temp == '{' || /*temp == '}' ||*/ temp == '<' /*|| temp == '>'*/)
			{
				cout << temp;
				stck.push(temp);
			}
			else if (temp == ')' || temp == ']' || temp == '}' || temp == '>')
			{
				cout << temp;

				if (temp == ')' && stck.top() == '(')
				{
					stck.pop();
				}
				else if (temp == ']' && stck.top() == '[')
				{
					stck.pop();
				}
				else if (temp == '}' && stck.top() == '{')
				{
					stck.pop();
				}
				else if (temp == '>' && stck.top() == '<')
				{
					stck.pop();
				}
				else
				{
					stck.push(temp);
				}
			}
		}
		temp = infile.get();
	}

	temp = infile.get();

	if (stck.empty())
	{
		cout << "\nMismatched" << endl;
	}
	else
	{
		cout << "\nMatched" << endl;
	}
	cout << endl << endl;;
	stck.display();
	stck.clear();
	}

	cout << endl << endl;
}


Output:
Input string:
()
Matched




Input string:
[](){}
Matched




Input string:
[{()}]
Matched




Input string:
<><<<>>>
Matched




Input string:
[[))
Mismatched


[ [ ) )

Input string:
{()[()]}
Mismatched


{ ╠ [ ) ] }



Press any key to continue . . .
> I only included my function but if I need to include more of my program I will.
Post enough code to reproduce your problem.


When you have a `closing' sign, you check the top of the stack ¿what if the stack is empty?

Also, I recommend you to use helper functions
1
2
3
4
5
6
7
8
9
10
if( is_sign(temp) ){
   if( is_openning_sign(temp) )
      stack.push(temp);
   else{ //no need to check is_closing_sign
      if( not stack.empty() and match(temp, stack.top()) )
         stack.pop()
      else
         stack.push(temp);
   }
}
if the stack is empty then it won't have a match and it will just push the closing sign on top of the stack causing it to have something in it when I check to see if it is empty.

If I use the helper functions like you wrote will it make my code work better or will it just be simplifying what I have?

Here is my entire 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*Matthew Brooks
2/12/2012
This is a stack program
*/

#include<iostream>
#include<fstream>
using namespace std;



	class List
	{

	public:
		typedef char vT;
		static const size_t CAPACITY = 50;

		List()
		{
			used = 0;
			pos = 0;
			for (int x = 0; x < CAPACITY; x++)
			{
				data[x] = 0;
			}
		}

		List(const List &l)
		{
			used = l.used;
			pos = l.pos;

			for (int x = 0; x < used; x++)
			{
				data[x] = l.data[x];
			}
		}

		bool empty(){ return used; }

		void front(){ pos = 0; }

		void end(){ pos = used - 1; }

		void prev()
		{
			if (used > 0)
			{
				pos = pos - 1;

				if (pos < 0)
				{
					pos = 0;
				}
			}
		}

		void next()
		{
			if (used > 0)
			{
				pos = pos + 1;

				if (pos >= used)
				{
					pos = used - 1;
				}
			}
		}

		size_t getPos(){ return pos; }

		void setPos(int x)
		{
			if (x >= 0 || x < used)
			{
				pos = x;
			}
		}

		void insertBefore(int v)
		{

			vT temp[CAPACITY];
			int i = 0;

			if (used == 0)
			{
				data[pos] = v;
				used++;
			}
			else
			{
				if (pos < 0 || pos > CAPACITY)
				{
					cout << "Can not insert" << endl;
				}
				else
				{
					for (int x = pos; x < used; x++)
					{
						temp[i] = data[x];
						i++;
					}

					data[pos] = v;

					i = 0;

					for (int x = pos + 1; x < used; x++)
					{
						data[x] = temp[i];
						i++;
					}


					used++;
				}

				if (used > CAPACITY)
				{
					used = CAPACITY;
				}
			}
		}

		void insertAfter(int v)
		{
			vT temp[CAPACITY];
			int i = 0;

			if (used == 0)
			{
				data[pos] = v;
				used++;
			}
			else
			{
				if (pos > CAPACITY || pos < 0)
				{
					cout << "Can not insert" << endl;
				}
				else
				{
					for (int x = pos + 1; x < used; x++)
					{
						temp[i] = data[x];
						i++;
					}

					data[pos + 1] = v;

					i = 0;

					for (int x = pos + 2; x < used; x++)
					{
						data[x] = temp[i];
						i++;
					}

					pos = pos + 1;

					used++;

					if (used > CAPACITY)
					{
						used = CAPACITY;
					}
				}
			}
		}

		vT getElement(){ return data[pos]; }

		size_t size(){ return used; }

		void replace(int v){ data[pos] = v; }

		void erase()
		{
			vT temp[CAPACITY];

			if (used == 0)
			{
				return;
			}
			else
			{
				for (int x = 0; x < pos; x++)
				{
					temp[x] = data[x];
				}
				int i = pos;

				for (int x = pos + 1; x < used; x++)
				{
					temp[i] = data[x];
					i++;
				}

				for (int x = 0; x < used; x++)
				{
					data[x] = temp[x];
				}
				used--;
			}
		}

		void clear(){ used = 0; pos = 0; }

		void reverse()
		{
			vT temp[CAPACITY];

			for (int x = 0; x < used; x++)
			{
				temp[x] = data[x];
			}

			int i = 0;

			for (int x = used - 1; x >= 0; x--)
			{
				data[i] = temp[x];
				i++;
			}
		}

		void swap(List& l2)
		{
			vT temp1[CAPACITY];
			vT temp2[CAPACITY];

			for (int x = 0; x < used; x++)
			{
				temp1[x] = data[x];
			}

			for (int x = 0; x < l2.used; x++)
			{
				temp2[x] = l2.data[x];
			}

			for (int x = 0; x < l2.used; x++)
			{
				data[x] = temp2[x];
			}

			for (int x = 0; x < used; x++)
			{
				l2.data[x] = temp1[x];
			}
		}

		friend ostream& operator<<(ostream& outs, const List& l1)
		{
			for (int x = 0; x < l1.used; x++)
			{
				outs << l1.data[x] << " ";
			}
			outs << endl;
			return outs;
		}

		List operator +(List l2)
		{
			List temp;

			temp.used = used + l2.used;

			for (int x = 0; x < used; x++)
			{
				temp.data[x] = data[x];
			}
			int i = 0;
			for (int x = used; x < l2.used; x++)
			{
				temp.data[x] = l2.data[i];
				i++;
			}
			return temp;

		}

		void operator =(List l2)
		{
			used = l2.used;
			pos = l2.used;

			for (int x = 0; x < used; x++)
			{
				data[x] = l2.data[x];
			}
		}

		bool operator ==(List l2)
		{
			bool sum = 1;

			if (used != l2.used)
			{
				sum = 0;
			}

			int count = 0;

			while (sum != 0 && count < used)
			{
				if (data[count] != l2.data[count])
				{
					sum = 0;
				}
				count++;
			}

			return sum;
		}

	private:
		vT data[CAPACITY];
		size_t used;
		size_t pos;
	};

	class stack
	{
	private:
		List mystack;

	public:
		typedef char vT;

		void push(vT v)
		{
			//mystack.end();
			mystack.insertAfter(v);
		}

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

		vT top()
		{
			mystack.end();
			return mystack.getElement();
		}

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

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

		void display()
		{
			cout << mystack << endl;
		}
	};

void readFile(stack &);

int main()
{
	stack stck;
	readFile(stck);


	return 0;
}

void readFile(stack &stck)
{
	fstream infile;
	char temp = 'a';
	infile.open("first.txt");

	temp = infile.get();

	while (temp != '\n')
	{
	cout << "Input string:" << endl;
	
	while (temp != '\n')
	{

		if (temp == '(' || temp == ')' || temp == '[' || temp == ']' || temp == '{' || temp == '}' || temp == '<' || temp == '>')
		{
			if (temp == '(' || /*temp == ')' ||*/ temp == '[' || /*temp == ']' ||*/ temp == '{' || /*temp == '}' ||*/ temp == '<' /*|| temp == '>'*/)
			{
				cout << temp;
				stck.push(temp);
			}
			else if (temp == ')' || temp == ']' || temp == '}' || temp == '>')
			{
				cout << temp;

				if (temp == ')' && stck.top() == '(')
				{
					stck.pop();
				}
				else if (temp == ']' && stck.top() == '[')
				{
					stck.pop();
				}
				else if (temp == '}' && stck.top() == '{')
				{
					stck.pop();
				}
				else if (temp == '>' && stck.top() == '<')
				{
					stck.pop();
				}
				else
				{
					stck.push(temp);
				}
			}
		}
		temp = infile.get();
	}

	temp = infile.get();

	if (stck.empty())
	{
		cout << "\nMismatched" << endl;
	}
	else
	{
		cout << "\nMatched" << endl;
	}
	cout << endl << endl;;
	stck.display();
	stck.clear();
	}

	cout << endl << endl;
}
> if the stack is empty then it won't have a match
if the stack is empty, ¿what value should `top()' return?

bool empty(){ return used; }
¿your empty() functions returns true if the list has elements?
that's obfuscated



`erase()' does not update `pos', so when you try to `push()' later, the element ends in a wrong position

Breakpoint 1, stack::push (this=0x7fffffffe0c0, v=123 '{') at foo.cpp:212
212                             mystack.insertAfter(v);
$10 = {static CAPACITY = 50, data = '\000' <repeats 49 times>, used = 0, pos = 0}

Breakpoint 1, stack::push (this=0x7fffffffe0c0, v=40 '(') at foo.cpp:212
212                             mystack.insertAfter(v);
$11 = {static CAPACITY = 50, data = "{", '\000' <repeats 48 times>, used = 1, pos = 0}
(gdb)
Continuing.

Breakpoint 2, stack::pop (this=0x7fffffffe0c0) at foo.cpp:215
215                             mystack.end();
$12 = {static CAPACITY = 50, data = "{(", '\000' <repeats 47 times>, used = 2, pos = 1}
(gdb)
Continuing.

Breakpoint 1, stack::push (this=0x7fffffffe0c0, v=91 '[') at foo.cpp:212
212                             mystack.insertAfter(v);
$13 = {static CAPACITY = 50, data = "{", '\000' <repeats 48 times>, used = 1, pos = 1}
(gdb)
Continuing.

Breakpoint 1, stack::push (this=0x7fffffffe0c0, v=40 '(') at foo.cpp:212
212                             mystack.insertAfter(v);
$14 = {static CAPACITY = 50, data = "{\000[", '\000' <repeats 46 times>, used = 2, pos = 2}
(don't mind the line numbers, I've reformated your code)
see how in $13 used==pos and so in $14 data gets a character between `{' and `['


By the way, line 52: if (pos < 0) { warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]



> If I use the helper functions like you wrote will it make my code work better
> or will it just be simplifying what I have?
Your code would be clearer, easier to understand, easier to maintain
Last edited on
I wouldn't think it would matter what value top() returned because it will not be a match to one of the characters in my if statements so it will just push the closing sign on to the stack.

I see that my empty() function is kind of confusing but this is a carry over from functions that were given to me by my professor.

I get what you are saying about how pos doesn't change in erase() but if I decrement it I get all kinds of errors. Why is that? and why does it work for all my other input lines except the last one.

> I wouldn't think it would matter what value top() returned because it will not be a match
If you can't assure that it would return, by instance, 0, then ¿what does avoid it to return '('?
Suppose that you push a lot of parenthesis, then pop them all. `data' would be filled with '('

Another thing, ¿would you be accessing inside the array?

You are trying to access the top of an empty stack. That element does not exists, your operation is not valid, it is a logic error. You need to make sure that that never happens.


> I get what you are saying about how pos doesn't change in erase()
> but if I decrement it I get all kinds of errors. Why is that?
My magic cat is not with me right now. I need to see
- how you are decrement it
- what errors are you getting


> why does it work for all my other input lines except the last one.
because the other tests were easy
do a run through a debugger and watch carefully the state of `data' after you analyse each character.
I went through it like you said and I got it working. Thank you so much for the guidance and help!
Topic archived. No new replies allowed.