Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone plpease help me fix my code it prints infinitely when any value or s

ID: 666910 • Letter: C

Question

Can someone plpease help me fix my code it prints infinitely when any value or string is entered


#//The Header
const int 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>
using namespace 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!=" ")
       {
           return false;
       }
   }
   return true;
}
bool CQueue::IsFull()
{
   for(int x=0;x<3;x++)
   {
       if(passengers[x].name==" ")
       {
           return false;
       }
   }
   return true;
}
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"
using namespace std;

enum choice { BOOKED, WAITING };
const int 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 ";
cout << "======== ";
cout << "1. Add Passenger ";
cout << "2. Delete Passenger ";
cout << "3. Show Passengers ";
cout << "4. Exit ";
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;
   }
else if(theQueue[1].IsEmpty()==false)
{
       cout<<"Please type in the name of the new passenger.";
       cin>>theString.name;
       theQueue[1].Enqueue(theString);
}
else if((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);
}
else if(theQueue[2].IsEmpty()==false)
{
       cout<<"Please type in the name of the new passenger.";
       cin>>theString.name;
       theQueue[2].Enqueue(theString);
}
else if((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;
   }
}

Explanation / Answer

Few notes on why a code enters an infinite loop:

Let us see your code:

A code tree (which function calls which other function ) would help to debug the code easily:

main() -> ShowMenu(), AddPassengers(), DeletePassengers() and ShowPassengers();

Places in code that uses printing (cout statement):

Passenger CQueue::Front()
{
   cout<<"Outputting the passenger list: Booked Passengers:"<<endl;    // 1. ?
   return passengers[1];
}
and many other places.

Likely corrections that may be needed:

       cin>>theString.name;       // read in value from user

theQueue[1].Enqueue(theString); // call the Enqueue() function with name as parameter

void CQueue::Enqueue(Passenger thePassenger)
{   passengers[front]=thePassenger;   if(front!=rear) { rear--; } front++;   }

// if there is a single item in Q, then add at front else decrementing rear – how can it be a real Enqueue() functionality? When you decrement the rear of the queue how can you add items in to the queue? Decrementing means removing items – correct?

Hence, the if statement

if(front!=rear) { rear--; } front++;

should be changed to something like this:

if(front!=rear) { rear++; } front++;

Received the following errors at compilation time:

The header file cqueue.h not found

If you had included the definition for the header file cqueue.h in the question, I might have tested the whole code. Sorry. At least if the question had more than 2 hours allotted, I could have written a new header file.