Hey everyone, I'm having some trouble writing this program using queues. The main issue that I'm having right now is that I have three queues, each is size=5. The user inputs a number to remove from a queue. The program will pop all the values out of the 3 queues to temporary queues, remove the number that was input, and then put the numbers back, so that queue 1 is never (!(full_queue)) if queue 2 is (!(empty_queue).

So like if you have
1-2-3-4-5
6-7-0-0-0
0-0-0-0-0

and you remove 3, you're left with
1-2-4-5-6
7-0-0-0-0
0-0-0-0-0

What's happening is that if I keep it simple and put 1-2-3-4-5 in queue 1 and delete 4, it shows me that q1 contains 2-3-5-4-5 when I do a.queue[0] --> and so on.

However, I made a counter to count how many items are in q1, and it will tell me 4. Is there some reason outputting all the 5 slots is showing that the slot I deleted really isn't empty and all the numbers are jumbled around?

Please post the code you have so far. We cant see your screen from here.

Sure, it's a little long so that's why I didn't originally post it. If you'd like me to explain a particular part or show you where I'm having trouble, just ask.

/*This is the PRIMARY FILE for the garage project.
=============================================================*/

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

const int maxqueue = 5;
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[6];
	int front, rear;
};
	queue_type a, b, c, a2, b2, c2;

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

int main()
{
	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)
				{
					cout << "Car with plate " << x << " has left Garage 1." << endl;
				}
				if (!(x == q))
				{
					a.insert_queue(q);
				}//endelse
		}//endwhile

		while (!(b2.empty_queue())) //B queue filling
		{
			b2.delete_queue(q);
				if (x == q)
				{
					cout << "Car with plate " << x << " has left Garage 2." << 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'))
	{
		cout << a.queue[0] << endl;
		cout << a.queue[1] << endl;
		cout << a.queue[2] << endl;
		cout << a.queue[3] << endl;
		cout << a.queue[4] << endl << endl;

		cout << b.queue[0] << endl;
		cout << b.queue[1] << endl;
		cout << b.queue[2] << endl;
		cout << b.queue[3] << endl;
		cout << b.queue[4] << endl;

		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;
		g = 0;
		}
		else
		{
			cout << "\n\nYou are leaving the program with NO cars in either garage, or the street." << 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;
		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;
		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];
}
//=============================================================

I ran the code through my debugger and it looks like values are not being reset like they should. I also think you need to null out an element after it is deleted. That should help to find out what is going on. If you are using an IDE I really suggest you step through the code and look at the behavior.

My arrival code is working fine, I just cant get the departure code to work correctly. Something is getting really mixed up when queues a, b, c pop out to a2, b2, c2, and then back to a, b, c. Shouldn't just deleting the element clear the slot in the queues? I think something in the reinsertion loops is putting numbers in multiple times. Haven't used an IDE before.

I would try MSVS 2010 express edition. The debugger is very nice.

Well I'm using MSVS 2010 express but I have no experience with debugging, I always skip it. I'll play around for a while with it though. Does my departure code look all right to you or is there something that stands out that would cause it not to work? To me it seems like it should logically make sense. Pop to the temporary queues, put them back in original queues if (input != the element going back in) and follow all arguments about the queues being full or not.

One thing i see right away is that maxqueue should be 6 and not 5. when you check to see if front or back is 5 and if it is set it back to 0 you are never going to read queue[5].

But then when I input numbers all the queues accept 6 entries.

Your arrays are defined as six elements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.