Why is queue front something weird?

I cant figure out why my program is saying that the front of my queue is something that it shouldn't be. Without posting the whole long code, what could possibly be wrong with this. (I'm not using #include <queue>, just class queue_type)

Input

a.clear_queue();			
cout << "\nEnter a license number ==> ";
cin >> x;


Putting it in a.queue
	if (!(a.full_queue()))
	{
		a.insert_queue(x);
		cout << "Vehicle has been parked in Garage 1." << endl;
		cout << "============================================================================" << endl << endl;
		break;
	}


Then if I enter the number 1 as input
		cout << a.front << endl;

Tells me that the front of queue a is 5.
Last edited on
Without posting the whole long code, what could possibly be wrong with this.


Without posting the whole long code, what could we possibly know about your queue_type class?
Am I getting messed up because I need to make my queues 1 size larger to account for front being dead space? It keeps telling me that my front == whatever number i set my int maxqueue to.
Again, we can't know without the code. It's like saying "Doctor! Doctor! My friend is sick! What's wrong with him?" "Can I see your friend?" "No, he's too boring."
Yeah You're right. I'm starting to get a feeling that it has to do with the voids at the bottom but I'm not sure. I honestly would love some direction with this front and rear issue.

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
/*This is the PRIMARY FILE for the garage project.
=============================================================*/

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int maxqueue = 6;
class queue_type
{
public:
	void clear_queue();
	bool empty_queue();
	bool full_queue();
	void insert_queue(int numb);
	void delete_queue(int& numb);
	int queue[7];
	int front, rear;
};
	queue_type a, b, c, a2, b2, c2;

//=============================================================

int main()
{
	srand((unsigned int)time(0));
	int tempdoll = 0; //used to establish and output a (rand()%5) * 10  value
	int dollars = 0; //TOTAL of all money collected
	int x = 0;
	int g = 0; //counter for exit output
	int q = 0; //temp value for queue popping
	char n; //n is arrive, depart, or exit;  x is numb number
	a.clear_queue();
	b.clear_queue();
	c.clear_queue();
	a2.clear_queue();
	b2.clear_queue();
	c2.clear_queue();

	//=============================================================
	// STARTUP CODE
	//=============================================================
	do
	{
	cout << "               Is a vehicle arriving or departing?\n" << endl;
	cout << "Car Arriving : A   ||   Car Departing : D   ||   Exit : X" << endl;
	cout << "Enter selection letter ==> ";
	cin >> n;



	if ((!(n=='x')) && (!(n=='X')))
	{
			cout << "\nEnter a license number ==> ";
			cin >> x;
	}


	//=============================================================
	// ARRIVE CODE
	//=============================================================
	while ((n=='a') || (n=='A'))
	{
	if (!(a.full_queue()))
	{
		a.insert_queue(x);
		cout << "Vehicle has been parked in Garage 1." << endl;
		cout << "============================================================================" << endl << endl;
		break;
	}
	else if (!(b.full_queue()))
	{
		b.insert_queue(x);
		cout << "Garage 1 is full.  Vehicle has been parked in Garage 2." << endl;
		cout << "============================================================================" << endl << endl;
		break;
	}
	else if (!(c.full_queue()))
	{
		c.insert_queue(x);
		cout << "Both garages are full.  Vehicle has been parked on the street." << endl;
		cout << "============================================================================" << endl << endl;
		break;
	}
	else 
	{
		cout << "All garages and the street are full." << endl << endl;
		cout << "============================================================================" << endl << endl;
		break;
	}
	}//End while

	//=============================================================
	// DEPART CODE
	//=============================================================
	if ((n=='d') || (n=='D')) //n is decision, x is plate number
	{
		while (!(a.empty_queue()))
		{
			a.delete_queue(q);
			a2.insert_queue(q);
		}//end while

		while (!(b.empty_queue()))
		{
			b.delete_queue(q);
			b2.insert_queue(q);
		}//end while

		while (!(c.empty_queue()))
		{
			c.delete_queue(q);
			c2.insert_queue(q);
		}//end while

		while (!(a2.empty_queue())) //A queue filling
		{
			a2.delete_queue(q);
				if (x == q)
				{
					tempdoll = (rand()%5) * 10;
					dollars = dollars + tempdoll;
					cout << "Car with plate " << x << " has left Garage 1." << endl;
					cout << "Vehicle has been charged $" << tempdoll << " for parking." << endl;
				}
				if (!(x == q))
				{
					a.insert_queue(q);
				}//endelse
		}//endwhile

		while (!(b2.empty_queue())) //B queue filling
		{
			b2.delete_queue(q);
				if (x == q)
				{
					tempdoll = (rand()%5) * 10;
					dollars = dollars + tempdoll;
					cout << "Car with plate " << x << " has left Garage 2." << endl;
					cout << "Vehicle has been charged $" << (rand()%5) * 10 << " for parking." << endl;
				}
				if (!(x == q))
				{
					if (!(a.full_queue()))
					{
						a.insert_queue(q);
					}
					
					if ((a.full_queue()) && (!(b.full_queue())))
					{
						b.insert_queue(q);
					}//endif
				}//endif
		}//endwhile

		while (!(c2.empty_queue())) //C queue filling
		{
			c2.delete_queue(q);
				if (x == q)
				{
					cout << "Car with plate " << x << " has left the street." << endl;
				}
				if (!(x == q))
				{
					if (!(b.full_queue()))
					{
						b.insert_queue(q);
					}
					
					if ((b.full_queue()) && (!(c.full_queue())))
					{
						c.insert_queue(q);
					}//endif
				}//endif
		}//endwhile
	}

	}//end do
	while ((!(n == 'x')) && (!(n == 'X')));
	// below is Garage 1 exit output
	if ((n == 'X') || (n == 'x'))
	{
		while (!(a.empty_queue()))
		{
			a.delete_queue(q);
			g++;
		}
		if (!(g == 0))
		{
		cout << "\n\nYou are leaving the program with " << g << " cars in Garage 1." << endl;
		cout << "You have collected $" << dollars << " total from parking fees." << endl;
		g = 0;
		}
		else
		{
			cout << "\n\nYou are leaving the program with NO cars in either garage, or the street." << endl;
			cout << "You have collected $" << dollars << " total from parking fees." << endl;
		}
	}
	
	// below is Garage 2 exit output
	if (((n == 'X') || (n == 'x')) && (!(b.empty_queue())))
	{
		while (!(b.empty_queue()))
		{
			b.delete_queue(q);
			g++;
		}
		cout << "You are leaving the program with " << g << " cars in Garage 2." << endl;
		cout << "You have collected $" << dollars << " total from parking fees." << endl;
		g = 0;
	}
	// below is the street exit output
	if (((n == 'X') || (n == 'x')) && (!(c.empty_queue())))
	{
		while (!(c.empty_queue()))
		{
			c.delete_queue(q);
			g++;
		}
		cout << "You are leaving the program with " << g << " cars on the street." << endl;
		cout << "You have collected $" << dollars << " total from parking fees." << endl;
		g = 0;
	}



	return 0;
}

//=============================================================
void queue_type::clear_queue()
{
	front = maxqueue;
	rear = maxqueue;
}
//=============================================================
bool queue_type::empty_queue()
{
	if (rear == front)
		return true;
	else
		return false;
}
//=============================================================
bool queue_type:: full_queue()
{
	int querear;
	if (rear == maxqueue)
		querear = 0;
	else
		querear = rear + 1;
	if (querear == front)
		return true;
	else
		return false;
}
//=============================================================
void queue_type::insert_queue(int numb)
{
	if (rear == maxqueue)
		rear = 0;
	else
		rear = rear + 1;
	queue[rear] = numb;
}
//=============================================================
void queue_type::delete_queue(int& numb)
{
	if (front == maxqueue)
		front = 0;
	else
		front = front + 1;
	numb = queue[front];
}
//============================================================= 
Last edited on
In the future, use [code][/code] tags and not quote tags.

Anyway, I'm struggling to understand your code and your queue class...it doesn't look like a queue at all, but I'm not sure what it is. Also, your naming conventions require a lot of extra typing...you don't need "_queue" as the end of your member functions for any reason. Also, what is the purpose of front and rear? I am struggling to understand what you are trying to do here :(
If I remove the "_queue" I get errors such as "class queue_type has no member 'full'". I'm using a format (which we were taught) which isn't the STL for queues and I think it's messing things up. I really don't understand why we're supposed to do work this way with queues and not use the STL pop & push method.
If this is what you're being taught to do, then sorry I can't help you. Maybe someone else who knows the proper way things should be can help you, but I don't want to get you bad grades or similar by doing something you're not supposed to. If I could understand your code maybe I could spot the problem(s), but I'm having a hard time diagnosing the way this all works.
Thanks for taking a look at it. I'm going to quickly redo this using the standard template and see how it works. I'm sure it will be ten times easier to read and figure out where the problems are. I'm not concerned with the grade; the prof gives us 2 sheets of example queues using the format I used and then gives us projects to do using them as a base, which usually gives huge amounts of problems. I'll post back soon with the new code.. well.. if I run into an issue that is *crosses fingers*

Edit: Can you define a queues size, as these must be able to hold only 5 elements... or do I need to use deque?
Last edited on
A queue implemented with a "circular" array.
You have two pointers: insert and delete.
It keeps telling me that my front == whatever number i set my int maxqueue to.
¿what did you expect? front is pointing to the first element. It's an index. If you want the element you need to do queue[front]

If I remove the "_queue" I get errors such as "class queue_type has no member 'full'"
Change all the calls, the declaration and the definition
Thank you for that. I literally was given two sheets of paper with a sample code of how to input and that was it so I'm trying to play catch up. Is there also a pointer for the back element, or do I just need to adjust my sizes according to the one blank space for the front pointer?
Isn't that what the rear member is for? ;)
Topic archived. No new replies allowed.