My professor is asking us to write a queue program, by which I mean that we are required to write out all of the functions of a queue like push, pop, etc. The program he asked us to write takes place in two .cpp files and one header file.
The professor provided some of the driver and the header files, but I still need to write the function itself, and add the code to a few functions on the driver. The header file is finished and cannot be modified. I wrote all of the necessary functions and the program does compile but I can't get it to do what it should.
//The Header
constint MAX = 3; //To do: determine appropriate number
struct Passenger {
char name[80];
};
class CQueue {
private:
int front;
int rear;
Passenger passengers[MAX];
public:
CQueue();
bool IsEmpty(void);
bool IsFull(void);
void Enqueue(Passenger);
Passenger Front(void);
void Dequeue(void);
};
//The Functions:
#include "cqueue.h"
#include <cstring>
#include <iostream>
usingnamespace std;
CQueue::CQueue()
{
for(int x=0;x<3;x++)
{
passengers[x].name==" ";
}
front=0;
rear=0;
}
bool CQueue::IsEmpty()
{
for(int x=0;x<3;x++)
{
if(passengers[x].name!=" ")
{
returnfalse;
}
}
returntrue;
}
bool CQueue::IsFull()
{
for(int x=0;x<3;x++)
{
if(passengers[x].name==" ")
{
returnfalse;
}
}
returntrue;
}
void CQueue::Enqueue(Passenger thePassenger)
{
passengers[front]=thePassenger;
if(front!=rear)
{
rear--;
}
front++;
}
Passenger CQueue::Front()
{
cout<<"Outputting the passenger list: Booked Passengers:"<<endl;
return passengers[1];
}
void CQueue::Dequeue()
{
for(int x=0;x<3;x++)
{
passengers[x].name==" ";
}
}
//The Driver:
#include <iostream>
#include <string>
#include <cstring>
#include "cqueue.h"
usingnamespace std;
enum choice { BOOKED, WAITING };
constint LINES = 2;
int showMenu(void);
void addPassenger(CQueue*);
void deletePassenger(CQueue*);
void showPassengers(CQueue*);
int main (void)
{
CQueue qPassengers[LINES];
int x;
do{
x = showMenu();
switch (x)
{
case 1: addPassenger(qPassengers);
break;
case 2: deletePassenger(qPassengers);
break;
case 3: showPassengers(qPassengers);
break;
}
} while (x != 4);
return 0;
}
int showMenu(void)
{
int select;
cout << "Menu\n";
cout << "========\n";
cout << "1. Add Passenger\n";
cout << "2. Delete Passenger\n";
cout << "3. Show Passengers\n";
cout << "4. Exit\n";
cout << "Enter choice: ";
cin >> select;
return select;
}
//void addPassenger(CQueue*);
//void deletePassenger(CQueue*);
//void showPassengers(CQueue*);
void addPassenger(CQueue *theQueue)
{
Passenger theString;
if((theQueue[1].IsFull()==false) && (theQueue[2].IsFull()==false))
{
cout<<"Both queues are full."<<endl;
}
elseif(theQueue[1].IsEmpty()==false)
{
cout<<"Please type in the name of the new passenger.";
cin>>theString.name;
theQueue[1].Enqueue(theString);
}
elseif((theQueue[1].IsEmpty())==true && (theQueue[1].IsFull())==true)
{
cout<<"Please type in the name of the new passenger.";
cin>>theString.name;
theQueue[1].Enqueue(theString);
}
elseif(theQueue[2].IsEmpty()==false)
{
cout<<"Please type in the name of the new passenger.";
cin>>theString.name;
theQueue[2].Enqueue(theString);
}
elseif((theQueue[2].IsEmpty())==true && (theQueue[2].IsFull())==true)
{
cout<<"Please type in the name of the new passenger.";
cin>>theString.name;
theQueue[2].Enqueue(theString);
}
}
void deletePassenger(CQueue *theQueue)
{
theQueue[1].Dequeue();
}
void showPassengers(CQueue *theQueue)
{
for(int x=1;x<=2;x++)
{
cout<<theQueue[1].Front().name;
}
}
The way the program is supposed to work is to create two CQueue objects with one of them as a list of passengers on the plane and one as a waiting list. If the plane is full, passengers go on the waiting list (which also has a limit).
When I try to make the program show passengers it doesn't work (outputs a huge amount of random characters) and because of this I can't really tell if add or delete passengers works.