Exception. need help fixing.

Good evening forum!
I have a lab assignment that I'm breaking my head for a while on now.
I am a CSC student, and I'm still getting used to C++ programming.
I have an assignment to simulate a parking lot, with 2 lanes, 10 spaces in each and street. I have to use Array stack implementation that my Professor provided.
I read from a txt file, the letter A (Arrival) or D (Departure)and the plate number separated by blank in the following form:
Input file:
A DGW323
A GWT123
A GTW435
A ABC123
A GWR891
D GWT123
A GTX950
A FCM960
A DMG333
A DMG334
D GWR891
A DMG33d
A DMG330
A DMG33q
A DMG33w
A DMG33e
A DMG33r
A DMG33t
A DMG33y
A DMG33u
A DMG33o

Problem is, the program executes Arrivals fine, but when there is a departure, it pops all the cars from the first garage into the street and breaks due to an exception. I can't figure out the problem.
I'll post the code, if any of you can please take a look.
Thank you.

stack.h (header 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
// file Stack.h
// array stack implementation
#ifndef Stackh
#define Stackh
#include <cstdlib>

template<class StackType>
class Stack {
	// LIFO objects

public:
	Stack(int MaxStackSize = 10);
	~Stack() { delete[] stack; }
	bool IsEmpty() const { return top == -1; }
	bool IsFull() const { return top == MaxTop; }
	StackType Top() const;
	void push(const StackType & x);
	void pop();
private:
	int top;    // current top of stack
	int MaxTop; // max value for top
	StackType *stack;   // element array
};

template<class StackType>
Stack<StackType>::Stack(int MaxStackSize)
{
	//Pre: none'
	//Post: Array of size MaxStackSaize to implement stack
	// Stack constructor.
	MaxTop = MaxStackSize - 1;
	stack = new StackType[MaxStackSize];
	top = -1;
}

template<class StackType>
StackType Stack<StackType>::Top() const
{
	//Pre: stack is not empty
	// Post:  Returns top element.
	if (IsEmpty())
		throw logic_error("Top fails: Stack is empty");// Top fails
	return stack[top];
}

template<class StackType>
void Stack<StackType>::push(const StackType & x)
{
	//Pre: Stack is not full
	//Post: Push x to stack.
	//		Stack has one more element
	if (IsFull()) throw logic_error("Push fails: full stack"); // Push fails
	stack[++top] = x;
}

template<class StackType>
void Stack<StackType>::pop()
{
	//Pre: Stack is not Empty
	//Post: Stack has one less element
	if (IsEmpty()) {
		throw logic_error("Pop fails: Stack is empty");
	}; // Pop fails
	top--;
}

#endif 



Source.cpp :

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
#include "stack.h"
#include<iostream>
#include<fstream>
#include<string>
#include <cstdlib>

using namespace std;

void arriving1(string &a);
void arriving2(string &a);
void departing1(string &a);
void departing2(string &a);
struct car
{
	char ad;
	string plate;
	int num_moves;
};

	int main()
{
	
	Stack <string> First;
	
	Stack <string> Second;
	
	Stack <string> Street;
	
	car cars;

	car temp;

	string *ptr1; 




	fstream textfile;
	textfile.open("Text.txt");
	


	while (!textfile.eof())
	{
		textfile >> cars.ad >> cars.plate;

		if (cars.ad == 'A')
		{
			if (First.IsFull())
			{
				if (Second.IsFull())
				{
					cout << "***********************************************************************" << endl;
					cout << "The Parking lot is full." << endl;
					cout << "License: " << cars.plate <<" Can't park in."<< endl;
					cout << "***********************************************************************" << endl;
				}
				else {
					arriving2(cars.plate);
					 Second.push(cars.plate);
				}
			}
			else
			{
			
				arriving1(cars.plate);
				First.push(cars.plate);
			}
		}
		if (cars.ad == 'D' && !First.IsEmpty())
		{
			
				while (!First.IsEmpty() || First.Top() != cars.plate)
			{
				First.pop();
				temp.plate =First.Top();
				Street.push(temp.plate);       //backing cars from garage to street until finding the matching license car
			}
				if(First.IsEmpty())
				{
					while (!Street.IsEmpty())
					{
						Street.pop();
						temp.plate = Street.Top();
						First.push(temp.plate);
					}

					temp.plate = cars.plate;
					while (!Second.IsEmpty() || temp.plate != cars.plate)
					{
						Second.pop();
						temp.plate = Second.Top();
						Street.push(temp.plate);
					}
				}


				else
				{
					departing1(cars.plate);
					Street.pop();
				}
			}
		while (!Street.IsEmpty())
		{
			Street.pop();
			temp.plate = Street.Top();
			Second.push(temp.plate);
		}
			
		}
	
	


	system("pause");
	return 0;
}

	void arriving1(string &a) //prints 
	{
		cout << "***********************************************************************" << endl;
		cout << "car has been parked in garage number 1." << endl;
		cout << "license plate number: " << a << endl;
		cout << "***********************************************************************" << endl;

	}

	void arriving2(string &a) //prints 
	{
		cout << "***********************************************************************" << endl;
		cout << "car has been parked in garage number 2." << endl;
		cout << "license plate number: " << a << endl;
		cout << "***********************************************************************" << endl;
	}

	void departing1(string &a)//prints 
	{
		cout << "***********************************************************************" << endl;
		cout << "car has been departured from garage number 1." << endl;
		cout << "license plate number: " << a << endl;
		cout << "***********************************************************************" << endl;
	}

	void departing2(string &a)//prints 
	{
		cout << "***********************************************************************" << endl;
		cout << "car has been departured from garage number 2." << endl;
		cout << "license plate number: " << a << endl;
		cout << "***********************************************************************" << endl;
	}


P.S
the program shows the exception on line 43 on the header file

return stack[top];

and breaks when its inside the while loop, while popping the first garage "First".

Thank you for reading
Last edited on
Hi,

73
74
75
while (!First.IsEmpty() || First.Top() != cars.plate)
			{
				First.pop();


If the stack isn't empty, all of the items will be popped. With an or condition only one of the expressions has to be true - the others are not evaluated. This is called short circuit evaluation, and logically makes sense.

If you are using a LIFO stack, it makes sense to pop things until you get to the item in question, would you then have to push them back in?

You are throwing exceptions, but not handling them. I don't see any try or catch statements.

Don't loop on eof, just loop on the stream itself, it knows when it is in a failed state:

1
2
while (textfile)
{


eof comes too late, it has already happened.

I would have a function to read the data into the container, and at least functions to process arriving and departing. At the moment you are combining all that into one giant block of logic.

Good Luck !!
Example from line 73:
1
2
3
4
5
6
while ( ! First.IsEmpty() || First.Top() != cars.plate ) // the || was already explained
{
  First.pop(); // ok, we did enter the loop because First is not empty (if using && in condition)
  temp.plate = First.Top(); // bad, this could now be nil or the sought car
  Street.push(temp.plate);
}

I.e. the order of your operations. Store the value first, before the car goes pop!
Thank you!
Topic archived. No new replies allowed.