Hi! I'm a BSCS student and currently working on an assignment about queues. We were tasked to solve a bank simulation problem. I got the header files for the teller and customer and both compiled successfully. Here are the header files:

Customer header file:

//Header file cust.h

#include <stdlib.h>
#include "boolean.h"

class Customer
{
   public:

      int arrival;
      int process;

      Customer (int at = 0)
	 : arrival (at),
	   process (2 + rand() % 6) {}

      //are we done with our transaction?
      bool done()
      {
	 return --process < 0;
      }

      //order by arrival time
      bool operator< (const Customer& c) const
      {
	 return arrival < c.arrival;
      }
};

Teller header file:

//Header file teller.h

#include "boolean.h"
#include "cust.h"

class Teller
{
   public:

      Teller (): free (TRUE) {}

      bool isFree() //are we free to service new customer?
      {
	 if(free)
	    return TRUE;
	 else if(customer.done())
	    free = TRUE;
	 return free;
      }

      //start serving new customer
      void addCustomer (const Customer &c)
      {
	 customer = c;
	 free = FALSE;
      }

   private:

      bool free;
      Customer customer;
};

I also used a header file queueA.h to implement the code using queues array implementation. Together with the header file is the implementation file queueA.cpp.

//Header file QueueA.h for the ADT queue
//Array-based implementation

const int MAX_QUEUE = 100;
typedef int queueItemType;

#include "boolean.h"

class queueClass
{
   public:
      
      //constructors and destructor:
      
      queueClass();
      queueClass(const queueClass &Q);
      ~queueClass();

      //queue operations:

      bool QueueIsEmpty();
      void QueueAdd(queueItemType NewItem, bool &success);
      void QueueRemove(bool &success);
      void GetQueueFront(queueItemType &QueueFront, bool &success);

   private:

      queueItemType Items[MAX_QUEUE];
      int Front;
      int Rear;
      int Count;
}; //end class
//End of header file
//Implementation file QueueA.cpp for the ADT queue
//Circular array-based implementation
//The array indexes to the front and rear of the queue.
//A counter tracks the number of items currently in the queue.

#include "QueueA.h" //header file

queueClass :: queueClass() : Front(0), Rear(MAX_QUEUE - 1), Count(0)
{
} //end constructor

queueClass :: queueClass(const queueClass &Q) : Front(Q.Front), Rear(Q.Rear), Count(Q.Count)
{
   for(int index = 0; index < Q.Count; ++index)
      Items[index] = Q.Items[index];
} //end copy constructor

queueClass :: ~queueClass()
{
} //end destructor

bool queueClass :: QueueIsEmpty()
{
   return bool(Count == 0);
} //end QueueIsEmpty

void queueClass :: QueueAdd(queueItemType NewItem, bool &success)
{
   success = bool(Count < MAX_QUEUE);

   if(success)
   {
      Rear = (Rear + 1) % MAX_QUEUE;
      Items[Rear] = NewItem;
      ++Count;
   } //end if
} //end QueueAdd

void queueClass :: QueueRemove(bool &success)
{
   success = bool(!QueueIsEmpty());

   if(success)
   {
      Front = (Front + 1) % MAX_QUEUE;
      --Count;
   } //end if
} //end QueueRemove

void queueClass :: GetQueueFront(queueItemType &QueueFront, bool &success)
{
   success = bool(!QueueIsEmpty());

   if(success)
      QueueFront = Items[Front];
} //end GetQueueFront
//End of implementation file

I'm starting to code the main program but i got a lot of errors when i compiled the code. Here's the code:

#include <iostream.h>
#include "QueueA.h"
#include "teller.h"

static unsigned long int lcg_seed = 0;

inline unsigned long int lcg_rand (int n)
{
   lcg_seed = ((lcg_seed * 11321) + 1) % 32575;
   return (int) (((double) lcg_seed / 32575.0) * n);
}

int main()
{
   const int numOfTellers = 5;
   const int numOfMins = 60;
   double totalWait = 0;
   int numOfCust = 0;

   Teller teller[numOfTellers];
   Customer cust[numOfMins];
   queueClass line;
   queueItemType front;
   bool success;

   for(int t = 0; t < numOfMins; t++)
   {
      if(lcg_rand (10) < 9)
	 line.QueueAdd(cust[numOfMins],success);

      for(int i = 0; i < numOfTellers; i++)
      {
	 if(teller[i].isFree() && !line.QueueIsEmpty(success))
	 {
	    Customer& frontCustomer = line.GetQueueFront(front, success);
	    numOfCust++;
	    totalWait += t - frontCustomer.arrival;
	    teller[i].addCustomer(frontCustomer);
	    line.QueueRemove(success);
	 }
      }
   }

   cout << "average wait: "
	       << (totalWait / numOfCust) << endl;

   return 0;
}

I really hope you can help me because I'm really confused.

Comments
Nice - post count == 1 and code tags, very nice

>>I'm starting to code the main program but i got a lot of errors when i compiled the code

Fix the errors one at a time, recompile after each fix to see what's really left. Compilers like to puke up 10-20 errors for the same line.

Post the first two or three errors so we can see what they are.


One problem I see is on line 10 of that last code snippet. The function is declared to return an unsigned int while line 10 returns an int. Wrong typecast.

Edited 6 Years Ago by Ancient Dragon: n/a

bool isFree() //are we free to service new customer?
      {
	 if(free)
	    return TRUE;
	 else if(customer.done())
	    free = TRUE;
	 return free;
      }

Rewrite it like this: There is a difference between TRUE and true; bool variable requires true, not TRUE

bool isFree() //are we free to service new customer?
 {
	  if(customer.done())
	    free = true;
	 return free;
      }
This article has been dead for over six months. Start a new discussion instead.