Hello everyone. My name is Mike. I am a new member :). I hope to be using this forum quite a bit to extend my knowledge base on c++ and java.(just wanted to introduce myself being my 1st post and all lol)

Well my class was tasked with making a queue program for a bank last week. This program was to simulate a line, with a certain amount of tellers. Calculating avg wait time etc etc. This week our professor wants us to print something like the following.....

Customer No Arrival time Waiting time in queue


Total waiting time: ____
Number of customers served: ______
Average waiting time: ______
After service, number of customers left in queue (left unserved): ________
*(The format is better shown in the code)^^^

Along with that addition we were tasked with making it so that no more customers were to be added after N minutes. The program runs for 20 minutes. Once 20minutes is over the rest of customers in queue continue to run.

I edited the code a bit to try to make these adjustments. I am not quite sure what route to take to achieve this... I asked my professor and he replied "Figure it out" lol. Any help would be great. Here is the code I am working with...

#include <iostream>
#include <queue>
#include <time.h>
using namespace std;


class Customer
{
private:
   int arrivalTime;
   int serviceTime;

public:
   Customer()
   {
      arrivalTime = 0;
      serviceTime = 0;
   }

   Customer(int arrival)
   {
      arrivalTime = arrival;
      serviceTime = rand()%9 + 2;
   }

   bool done()
   {
      return (serviceTime-- <= 0);
   }

   int getArrivalTime()
   {
      return arrivalTime;
   }
};

class Teller
{
private:
   Customer customerBeingServed;
   bool free;

public:
   Teller()
   {
      free = true;
      customerBeingServed = NULL;
   }

   void addCustomer(Customer cust)
   {
      customerBeingServed = cust;
      free = false;
   }

   bool isFree()
   {
      if (free) return true;
      if (customerBeingServed.done())
         free = true;
      return free;
   }
};


int main()
{
	/* initialize random seed: to generate a different sequence each time*/
	srand ( (unsigned int) time(NULL) );

	const int M = 3;  // Number of tellers
	const int N = 20; // Simulation time in minutes
	queue<Customer> customerQueue;
	Teller tellers[M];
	int totalWait = 0, numCustServed = 0;
	cout<<"Customer No.       Arrival time       Waiting time in queue"<<endl;
	cout<<"------------       ------------       ---------------------"<<endl;

	for (int currentTime = 0; currentTime < N; currentTime++)
	{
		if (1+rand()%10 <= 9)
		{
			 Customer customer(currentTime);
			 customerQueue.push(customer); 
			 cout<<"    "<<numCustServed+1<<"                   "<<currentTime<<"                        "<</* not sure what goes here*/<<endl;  
		}
		for (int t = 0; t < M; t++)
		 {
			if (tellers[t].isFree() && (!customerQueue.empty()))
			 {
				Customer customer = customerQueue.front();
				tellers[t].addCustomer(customer);
				totalWait = totalWait + (currentTime - customer.getArrivalTime());
				customerQueue.pop();
				numCustServed++;
			}
		} // end of for tellers
	} // end of for current time

	cout << "Total waiting time : " <<  totalWait << endl;
	cout << "Number of Customers served : "<< numCustServed<<endl;
	cout << "Average waiting time : " << totalWait/numCustServed<<endl;
	cout << "After service the number of customers left in queue : " <<endl;
	return 0;
}

Thanks in advance for the help I will receive. Thanks for reading :)

Well I have gone further with my code here is what I have now

#include <iostream>
#include <queue>
#include <time.h>
using namespace std;


class Customer
{
private:
int arrivalTime;
int serviceTime;

public:
Customer()
{
arrivalTime = 0;
serviceTime = 0;
}

Customer(int arrival)
{
arrivalTime = arrival;
serviceTime = rand()%9 + 2;
}

bool done()
{
return (serviceTime-- <= 0);
}

int getArrivalTime()
{
return arrivalTime;
}
};

class Teller
{
private:
Customer customerBeingServed;
bool free;

public:
Teller()
{
free = true;
customerBeingServed = NULL;
}

void addCustomer(Customer cust)
{
customerBeingServed = cust;
free = false;
}

bool isFree()
{
if (free) return true;
if (customerBeingServed.done())
free = true;
return free;
}
};

int totalWait;
int numCustServed;
int currentTime;
int main()
{
/* initialize random seed: to generate a different sequence each time*/
srand ( (unsigned int) time(NULL) );

const int M = 3; // Number of tellers
const int N = 20; // Simulation time in minutes
queue<Customer> customerQueue;
Teller tellers[M];
int totalWait = 0, numCustServed = 1;
cout<<"Customer No. Arrival time Waiting time in queue"<<endl;
cout<<"------------ ------------ ---------------------"<<endl;

for (currentTime = 0; currentTime < N; currentTime++)
{
if (1+rand()%10 <= 9)
{
Customer customer(currentTime);
customerQueue.push(customer);
cout<<"      "<<numCustServed<<"             "<<currentTime<<"               "<<totalWait/numCustServed<<endl;
}
for (int t = 0; t < M; t++)
{
if (tellers[t].isFree() && (!customerQueue.empty()))
{
Customer customer = customerQueue.front();
tellers[t].addCustomer(customer);
totalWait = totalWait + (currentTime - customer.getArrivalTime());
customerQueue.pop();
numCustServed++;
}

}
// end of for teller
} // end of for current time

cout << "\nTotal waiting time : " << totalWait << endl;
cout << "Number of Customers served : "<< numCustServed<<endl;
cout << "Average waiting time : " << totalWait/numCustServed<<endl;
cout << "After service the number of customers left in queue : "<< customerQueue.size()<<endl;


cout<<"==================================================================================================================="<<endl;
cout<<"\n\n\nCustomer No. Arrival time Waiting time in queue"<<endl;
cout<<"------------ ------------ ---------------------"<<endl;

//while (customerQueue.size()>0)
//{
//if (1+rand()%10 <= left)
//{
//Customer customer(currentTime);
//cout<<" "<<numCustServed<<" "<<currentTime<<" "<<totalWait/numCustServed<<endl;
//}

int t = M;
while (customerQueue.size()>0)
{
for (int t = 0; t < M; t++)
if (tellers[t].isFree() && (!customerQueue.empty()))
{
Customer customer = customerQueue.front();
customerQueue.pop();
cout<<"      "<<numCustServed<<"         "<<currentTime<<"              "<<totalWait/numCustServed<<endl;
t++;
numCustServed++;
}
}
cout << "\nTotal waiting time : " << totalWait << endl;
cout << "Number of Customers served : "<< numCustServed<<endl;
cout << "Average waiting time : " << totalWait/numCustServed<<endl;
cout << "After service the number of customers left in queue : "<< customerQueue.size()<<endl;

// end of for telle
 // end of for current time


return 0;

}

I have accomplished making the loop continue after the given time. The problem I am having now is that when I cout the customer number I get redundant information. EXAMPLE

Customer No.
=============
1
2
3
4
4
4
5
6
6
7
7
7
7
It should be incremented without the redundancy. Could someone help me with this?

Here's your code, formatted.

#include <iostream>
#include <queue>
#include <time.h>
using namespace std;


class Customer
{
private:
    int arrivalTime;
    int serviceTime;

public:
    Customer()
    {
        arrivalTime = 0;
        serviceTime = 0;
    }

    Customer(int arrival)
    {
        arrivalTime = arrival;
        serviceTime = rand()%9 + 2;
    }

    bool done()
    {
        return (serviceTime-- <= 0);
    }

    int getArrivalTime()
    {
        return arrivalTime;
    }
};

class Teller
{
private:
    Customer customerBeingServed;
    bool free;

public:
    Teller()
    {
        free = true;
        customerBeingServed = NULL;
    }

    void addCustomer(Customer cust)
    {
        customerBeingServed = cust;
        free = false;
    }

    bool isFree()
    {
        if (free) return true;
        if (customerBeingServed.done())
            free = true;
        return free;
    }
};

int totalWait;
int numCustServed;
int currentTime;
int main()
{
    /* initialize random seed: to generate a different sequence each time*/
    srand ( (unsigned int) time(NULL) );

    const int M = 3; // Number of tellers
    const int N = 20; // Simulation time in minutes
    queue<Customer> customerQueue;
    Teller tellers[M];
    int totalWait = 0, numCustServed = 1;
    cout<<"Customer No. Arrival time Waiting time in queue"<<endl;
    cout<<"------------ ------------ ---------------------"<<endl;

    for (currentTime = 0; currentTime < N; currentTime++)
    {
        if (1+rand()%10 <= 9)
        {
            Customer customer(currentTime);
            customerQueue.push(customer);
            cout<<"      "<<numCustServed<<"             "<<currentTime<<"               "<<totalWait/numCustServed<<endl;
        }
        for (int t = 0; t < M; t++)
        {
            if (tellers[t].isFree() && (!customerQueue.empty()))
            {
                Customer customer = customerQueue.front();
                tellers[t].addCustomer(customer);
                totalWait = totalWait + (currentTime - customer.getArrivalTime());
                customerQueue.pop();
                numCustServed++;
            }

        }
        // end of for teller
    } // end of for current time

    cout << "\nTotal waiting time : " << totalWait << endl;
    cout << "Number of Customers served : "<< numCustServed<<endl;
    cout << "Average waiting time : " << totalWait/numCustServed<<endl;
    cout << "After service the number of customers left in queue : "<< customerQueue.size()<<endl;


    cout<<"==================================================================================================================="<<endl;
    cout<<"\n\n\nCustomer No. Arrival time Waiting time in queue"<<endl;
    cout<<"------------ ------------ ---------------------"<<endl;

    //while (customerQueue.size()>0)
    //{
    //if (1+rand()%10 <= left)
    //{
    //Customer customer(currentTime);
    //cout<<" "<<numCustServed<<" "<<currentTime<<" "<<totalWait/numCustServed<<endl;
    //}

    int t = M;
    while (customerQueue.size()>0)
    {
        for (int t = 0; t < M; t++)
            if (tellers[t].isFree() && (!customerQueue.empty()))
            {
                Customer customer = customerQueue.front();
                customerQueue.pop();
                cout<<"      "<<numCustServed<<"         "<<currentTime<<"              "<<totalWait/numCustServed<<endl;
                t++;
                numCustServed++;
            }
    }
    cout << "\nTotal waiting time : " << totalWait << endl;
    cout << "Number of Customers served : "<< numCustServed<<endl;
    cout << "Average waiting time : " << totalWait/numCustServed<<endl;
    cout << "After service the number of customers left in queue : "<< customerQueue.size()<<endl;

    // end of for telle
    // end of for current time


    return 0;

}

You'll have repeats if line 97 isn't executed at least once every time you go through the outer loop starting on line 81. You have three tellers. You have a customer service time defined here:

serviceTime = rand()%9 + 2;

so if the first three customers all randomize to having long service times, seems like you should expect the 4th customer to have to wait, which means you should expect line 97 not to execute some times, so you should expect repeats. Seems like the output is about what I would expect.

>> It should be incremented without the redundancy.

Why? What is the output supposed to look like? Again, it looks like what I would expect for the code. Perhaps you want to move line 87 into the if statement inside lines 93 to 97? That way it will only print when a customer is served.

Thank you sooooo much. I see what your saying. Yes the instructor only wants the customers server to be on the list. :). I do how ever have just one more question.

serviceTime = rand()%9 + 2;

The above statement determines the amount of time they accept customers. I don't really understand the equation and how it determines the time. Could you explain it to me so that I can change the time interval that the bank accepts customers?

>> Thank you sooooo much.
You're welcome.

serviceTime = rand()%9 + 2;

rand() % 9 returns a random integer from 0 to 8, inclusive. Add 2 and you get a random number from 2 to 10, inclusive.

To get a random integer between a and b, inclusive, where a < b, do this:

rand() % (b - a + 1) + a;

So if you wanted a random number from 5 to 8 inclusive, it would be:

rand() % 4 + 5;

Thanks again VernonD. I had learned rand from a class last semester and just could not quite remember it. My program is now good to go. Thanks for showing the examples. :)

This question has already been answered. Start a new discussion instead.