473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

a line of Queue

33
Hi
First of all I should say, I dont want to bother any one with my question or long program
please if you have time, help me.( I dont mean that I want any one to do my program, just please if you are an expert and professional in C++, please help me and guid me how to fix the program)
I did my best to comment my program.


I am learning C++ and now I'm working on a program that:
reads in a file "customers.txt" containing the information for bank customers. The file is formatted with each customer's name, arrival time (in minutes after 9:00), and service time (in minutes) on a separate line. The program moves each customer into the bank line at the arrival time. If the teller counter is available, the customer is moved out of the line to the teller. When the customer has spent the specified amount of time at the counter, they leave the bank and the next customer steps up. The program continues until all customers are serviced.

I'm trying to make Multiple Tellers, at this time my program assumes the bank has only one teller/counter. I want to modify the program so that it can handle multiple tellers, so that the customers in line will move to any available teller. For example, "Chewbacca steps up to counter #2" or "Vader has left counter #4."
but I have no idea how I can do that:
right now my program works, but I dont know how to add multiple tellers.
my program is:

Expand|Select|Wrap|Line Numbers
  1. /****************************************************
  2. ** The main routine simulates a line-up at the bank.
  3. *****************************************************/
  4. int main() {
  5.     //Read in the customer file.
  6.     //Assume file is in format: Name ArrivalTime ServiceTime
  7.     //Assume list is sorted based on distinct arrival times.
  8.  
  9.     string file_name; // the files name that user input
  10.     ifstream fin;
  11.     cout<<"please Enter your choice:\nChoice1: (You can type in the name of your file.)\nchoice2: (You can ask the program to generate a list of random customers.)\n\n";
  12.  
  13.     int choice;
  14.     cout<<"what is your choice? (1 or 2):";
  15.     cin>>choice;
  16.     while(choice!=1 && choice!=2){
  17.         cout<<"You should type 1 or 2, try again:\nEnter you choice again:";
  18.         cin>>choice;
  19.     }
  20.     Queue<Customer> customerList;
  21.     Customer newCustomer;
  22.  
  23.     if(choice==1){ //choice 1 , user type his/her file_name to open
  24.         cout<<"PLease type your file name:";
  25.         cin >> file_name;
  26.         cout<<"\n";
  27.  
  28.         fin.open( file_name.c_str( ) );
  29.  
  30.  
  31.  
  32.         while (fin >> newCustomer)
  33.             customerList.enter(newCustomer);
  34.         fin.close();
  35.         if (customerList.isEmpty()) {
  36.             cout << "Could not read file customers.txt.\n";
  37.         return 0;
  38.         }
  39.     }
  40.  
  41.     if (choice==2){
  42.         cout<<"Please specify the probability that a customer arrives each minute:";
  43.         int probability;
  44.         cin>>probability;
  45.             for (int i=0; i <= 540; i++) {
  46.                 if ( 1+rand()%100 <= probability )
  47.                     customerList.enter( Customer("A customer", i, 5+rand()%11) );
  48.             }
  49.  
  50.  
  51.  
  52.     }
  53.     int num_teller; //number of tellers
  54.     cout<<"please Enter the Number of tellers:";
  55.     cin>>num_teller;
  56.     cout<<"\n";
  57.  
  58.  
  59.  
  60.     //The first customer immediately steps up to the counter, no waiting.
  61.     //We start the clock running at first customer's arrival time.
  62.     Customer atCounter = customerList.leave();
  63.     bool isCounterEmpty = false;
  64.     int currentTime = atCounter.get_arrivalTime();
  65.     int startService = atCounter.get_arrivalTime();
  66.     printTime(currentTime);
  67.     cout << "  " << atCounter.get_name() << " has entered the bank.\n";
  68.     printTime(currentTime);
  69.     cout << "  " << atCounter.get_name() << " stepped up to the counter.\n";
  70.  
  71.     //Set up our empty line.
  72.     Queue<Customer> line;
  73.  
  74.     //Repeat while there are customers in bank or yet to arrive.
  75.     while (!customerList.isEmpty() || !line.isEmpty() || !isCounterEmpty) {
  76.  
  77.         //Check if someone enters line.
  78.         if (!customerList.isEmpty() && (customerList.peek()).get_arrivalTime() <= currentTime) {
  79.             printTime(currentTime);
  80.             cout  << "  "<< (customerList.peek()).get_name() 
  81.                  << " has entered the bank.\n";
  82.             line.enter( customerList.leave() );
  83.         }
  84.  
  85.         //Check if someone leaves the counter.
  86.         if ( !isCounterEmpty && (startService + atCounter.get_serviceTime() <= currentTime) ) {
  87.             isCounterEmpty = true;
  88.             printTime(currentTime);
  89.             cout << "  " 
  90.                  << atCounter.get_name() 
  91.                  << " has left the counter.\n";
  92.         }
  93.  
  94.         //Check if counter is empty and someone is waiting in line.
  95.         if ( isCounterEmpty && !line.isEmpty() ) {
  96.             atCounter = line.leave();
  97.             startService = currentTime;
  98.             isCounterEmpty = false;
  99.             printTime(currentTime);
  100.             cout << "  " << atCounter.get_name() 
  101.                  << " has stepped up to the counter.\n";
  102.         }
  103.  
  104.         currentTime++;        //Inefficient.  Better to move to next event time.
  105.     }
  106.  
  107.     //Compute queue statistics.
  108.  
  109.  
  110.     return 0;
  111. }
Feb 19 '08 #1
7 3200
Banfa
9,065 Expert Mod 8TB
OK firstly a few site rules

1. Please do use code tags round your code
2. We don't allow posting of complete solutions to homework questions (this may not be homework for you but it certainly is for someone else)
3. Please read our posting guidelines where all this is explained

Onto your problem

Your program is limited by this declaration
Customer atCounter = customerList.leave();
This forces the program into only being able to deal with 1 customer at a time, i.e. only 1 teller. You will need to change this declaration in order to handle multiple tellers and then your will need to change you code to handle this variables new type.

Also why did you go to the bother of doing all that stuff creating your own Queue<class T> type when the C++ STD (Standard Template Library) already has a queue class in it?
Feb 19 '08 #2
APEJMAN
33
thanks alot for your help
sure, I'll read the guideline.
I am trying to learn C++, that's why I dont use the library.

take care
Feb 19 '08 #3
APEJMAN
33
should I work on the leave() function?
thanks
Feb 19 '08 #4
Banfa
9,065 Expert Mod 8TB
should I work on the leave() function?
thanks
There should be no need to work on the leave function as it works on a single entity (the line).

You may wish to google for the "sleeping barber problem" which this is a variation on.
Feb 19 '08 #5
APEJMAN
33
Atually I think that its better to have a vector like
vector<Customer> AtTeller(num_teller); // a vector including number of tellers

the num_teller is the number of tellers. btu how can I make a loop , so that the loop checkes if teller[i] is empty?

thanks
Feb 20 '08 #6
Banfa
9,065 Expert Mod 8TB
Atually I think that its better to have a vector like
vector<Customer> AtTeller(num_teller); // a vector including number of tellers

the num_teller is the number of tellers. btu how can I make a loop , so that the loop checkes if teller[i] is empty?
vector sounds right for your tellers, in fact when using the STL a common school of thought is that you should use vector unless you have a specific reason to use one of the other classes. vector is the class with the least overhead.

You have several options in making a loop, you could try using one of the Standard Library Algorithms, for_each or find_if look promising or you can just write a standard for loop

Expand|Select|Wrap|Line Numbers
  1. for(int ix=0; ix<AtTeller.size(); ix++)
  2. {
  3.     // Here perform operations on AtTeller[ix]
  4. }
  5.  
Feb 20 '08 #7
APEJMAN
33
I wrote this , but it dosent work
would you take a look at it, thanks.

Expand|Select|Wrap|Line Numbers
  1. for( int j=0; j<AtTeller.size(); j++){
  2. if (!line.isEmpty() /*&& isCounterEmpty*/){
  3.     if (AtTeller[i].get_name()=="DEFAULT"){
  4.         AtTeller[i]=line.leave();
  5.         startTime[i]=currentTime;
  6.         printTime(startTime[i]);
  7.         cout<<"  "<<AtTeller[i].get_name()<<" has sptepped up to the Counter #"<<i<<"\n";
  8.  
  9.     }
  10.  
  11. }
  12.  
  13. }
Feb 20 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: C. Alexander | last post by:
I'm making a 'whiteboard' application. I'm trying to have 2+ connected users able to draw at the same time. However, if User1 draws a line, when User2 is drawing, on User1 screen, it will draw...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
3
by: Kceiw | last post by:
Dear all, When I use #include "queue.h", I can't link it. The error message follows: Linking... G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In function `main':...
4
by: News | last post by:
Hi Everyone, The attached code creates client connections to websphere queue managers and then processes an inquiry against them. The program functions when it gets options from the command...
8
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
3
by: jrpfinch | last post by:
I have a script which is based on the following code. Unfortunately, it only works on Python 2.3 and not 2.5 because there is no esema or fsema attribute in the 2.5 Queue. I am hunting through...
4
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the...
0
by: kvnsmnsn | last post by:
Is there somewhere on-line I can go to find out how to use the stl queue? I'm working on a class assignment that includes inserting cha- racters onto a queue and dequeuing them from the queue, and...
0
by: ecestd | last post by:
I did implement the copy constructor but still have a problem with it. It is not working. What could be wrong? #include "QueueP.h" #include <cassert // for assert #include <new // for...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.