Need help :O

Okay so my program is suppose to have 3 lanes (Lane 1, lane 2, and street). I am suppose to push cars onto lane one once lane 1 is full then push to lane 2 if a car is departing I am suppose to push the cars on top of the selected car that is leaving until I hit the car I want. My only problem is I cant get the program to print departures and arrivals along with their license plates. I also cant get the count to work. The count is suppose to count how many times the car was moved.
My 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef LabTwoh2
#define LabTwoh2
#include <cstdlib>

#include "../car.h"
#include "Stack.h"

#include <string>

class Garage {

public:

	Garage()
	{
		Lane1 = new Stack<Car>;
		Lane2 = new Stack<Car>;
		Street = new Stack<Car>(100);
	}

	~Garage()
	{
		delete Lane1;
		delete Lane2;
		delete Street;
	}

	int search(string licence_plate) const;

	void move_to_street(int lane_number)
	{

		Car car;
		switch (lane_number)
		{
		case 1:
			car = Lane1->Top();
			Lane1->pop();
			++car;
			Street->push(car);
			break;

		case 2:
			car = Lane1->Top();
			Lane1->pop();
			++car;
			Street->push(car);
			break;
		}
	}

	void move_from_street(int lane_number)
	{
		Car car;
		switch (lane_number)
		{
		case 1:
			car = Street->Top();
			Street->pop();
			++car;
			Lane1->push(car);
			break;

		case 2:
			car = Street->Top();
			Street->pop();
			++car;
			Lane1->push(car);
			break;
		}
	}

	bool IsFull() const
	{
		return Lane1full() && Lane2full();
	}
	
	bool Lane1full() const
	{
		return Lane1->IsFull();
	}
	bool Lane2full() const
	{
		return Lane2->IsFull();
	}

	int search(string licence_plate)
	{
		int lane=0;

		if (Lane1->search(licence_plate))
		{
			lane = 1;
		}
		else if (Lane2->search(licence_plate))
		{
			lane = 2;
		}

		return lane;
	}

	void add_to_garage(Car c)
	{
		if (!Lane1->IsFull())
		{
			Lane1->push(c);
		}
		else if (!Lane2->IsFull())
		{
			Lane2->push(c);
		}
		else
		{
			cout << "Garage Full" << endl;
		}
	}

	void remove_from_garage(string licence_plate)
	{
		Car car;
		bool found = false;
		int lane = search(licence_plate);
		if (lane == 0)
		{
			return;
		}

		while (!found)
		{
			if (lane == 1)
			{

				car = Lane1->Top();

				if (car.get_plate() == licence_plate)
				{
					car = Lane1->Top();
					cout << car.get_plate() << ' ' << car.get_count() << endl;
					Lane1->pop();
					found = true;
				}
				else
				{
					move_to_street(1);
				}
			}

			if (lane == 2)
			{

				car = Lane2->Top();

				if (car.get_plate() == licence_plate)
				{
					car = Lane2->Top();
					cout << car.get_plate() << ' ' << car.get_count() << endl;
					Lane2->pop();
					found = true;
				}

				else
				{
					move_to_street(2);
				}

			}
		}

		while (!(Street->IsEmpty()))
		{
			move_from_street(lane);
		}
	}
	


private:

	Stack<Car> * Lane1;
	Stack<Car> * Lane2;
	Stack<Car> * Street;

};

#endif 


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
#include <fstream>
#include <iostream>
#include <string>
#include "../car.h"
#include "Garage.h"
#include "Stack.h"

using namespace std;

int main()
{
	
	ifstream filein("CarData.txt");
	Garage myGarage;
	string licence;
	char destination;


	while (!filein.eof())
	{
		filein >> destination >> licence;

		switch (destination)
		{
		
		case 'A':

			myGarage.add_to_garage(Car(licence));
			
			break;

		case 'D':

			myGarage.remove_from_garage(licence);

			break;
		}
	}



	filein.close();
	return 0;
}


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

#ifndef car_h
#define car_h

#include <string>

using namespace std;

class Car {

public:

	Car() 
	{ 
		count = 0; 
	}

	Car(string licence)
	{
		licence_plate = licence;
		count = 0;
	}

	bool operator==(string licence);
	bool operator==(Car car);

	Car operator=(Car car)
	{
		Car c(car.get_plate());
		c.set_count(car.get_count());
		return c;
	}
	
	int operator++();

	string get_plate() { return licence_plate; }
	int get_count() { return count; }
	void set_count(int c) { count = c; }

private:

	string licence_plate;
	int count;
};

bool Car::operator==(string licence)
{
	return licence_plate == licence;
}

bool Car::operator==(Car car)
{
	return licence_plate == car.licence_plate;
}


int Car::operator++()
{
	count++;
	return count;
}

#endif


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

#ifndef LabTwo
#define LabTwo
#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; }
	bool search(StackType value) const;
	StackType Top() const;
	void push(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(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");
		exit(1);
	}; // Pop fails
	top--;
}

//my search function

template<class StackType>
bool Stack<StackType>::search(StackType value) const
{
	for (int i = 0; i < top; i++)
	{
		if (stack[i] == value)
		{
			return true;
		}
	}

	return false;
}

#endif




Last edited on
Please use code tags. It's hard to help if you do not use them. It makes your code almost illegible.
Ok I put it into code tags format.
Last edited on
Topic archived. No new replies allowed.