Queue Project Problem

This is a discussion on Queue Project Problem within the C++ Programming forums, part of the General Programming Boards category; Hey guys I have yet another project. This ones focus is to get comfortable using queues. My problem is how ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    109

    Queue Project Problem

    Hey guys I have yet another project. This ones focus is to get comfortable using queues. My problem is how should i store the rider data into the queue, such as should i use get() or what, also what type of queue would it be..string, or int...Can I put all 3 parts of the rider day in the same spot in the queue and if so how would i access each one independently? Here is what the project entails.
    Project:

    Geek Faire is an amusement park that caters to technologically-oriented
    individuals. To ride one particular ride, The Touring Machine, customers
    arrive with a fixed number of tickets and are placed at the end of the
    line. Upon reaching the front of the line they pay and enter the
    ride. Only one person can ride at a time, and a single ride costs one
    ticket. The customer at the front of the line rides and completes the
    ride one time unit later. New customers may arrive during this time unit.
    A customer who just completed a ride and has more tickets is placed at
    the end of the line after the new customers. Upon running out of tickets
    the customer exits the ride. Your program will calculate the time unit
    during which each customer exits the ride.

    INPUT FORMAT:

    Input will come from a file called "riders.dat". This file will contain
    one or more lines of the form

    N E T

    N, E and T are separated by a single blank space. N is the last name of
    the rider, T is the number of tickets they have and E is the time unit
    during which they enter the line. N is an alphabetic string with no
    embedded whitespace, E is a non-negative integer and T will be an integer
    greater than 0. Riders are guaranteed to be ordered by entry time in the
    input file. The end of input is indicated by a line containing "END 0 0".

    OUTPUT FORMAT:

    Output will be written to a file called "riders.out". Each line of
    output will indicate the time the customer exits the ride. Lines will
    be of the form

    N E

    where N is the last name of the rider and E is the time unit during
    which they exit the ride. N and E are separated by a single blank
    space.

    EXAMPLE INPUT:

    Hopper 2 1
    Torvalds 3 10
    Jobs 8 4
    Gates 100 100
    END 0 0

    EXAMPLE OUTPUT:

    Hopper 3
    Jobs 16
    Torvalds 17
    Gates 200

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    I'd make it a queue of customer objects. A class/struct such as:
    Code:
    struct customer
    {
        std::string name;
        int entry_time;
        int num_tickets;
    };
    You can read the data from the file using >>:
    Code:
    customer temp;
    ifstream file("riders.dat");
    file >> temp.name >> temp.entry_time >> temp.num_tickets;
    If you wanted to get fancy you could overload the >> operator for the customer object which would simplify the read to just:
    Code:
    file >> temp;
    You store the struct into the queue using the basic queue operations such as push/pop. Is this an STL queue container we are talking about here?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    how about without structs just writing everything in main...can i read 3 things into 1 spot on the queue? Yes this is an STL container we're talking about. I know how to do the project just am unsure how to hold all 3 parts of the data into the same queue because some are datatype int and the other is a string.
    Last edited by DarkDot; 04-03-2007 at 05:02 PM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you are at the level of using STL containers and learning concepts such as queue, structs and functions should not be too much.
    The simplest way to put all the required data in the queue is using a struct.

    Or else you might convert everything into a string and store that. And if you want to access and modify some value, you'll need to take the string apart again, convert it into an int to do the operation, then convert it back and put together again. Does that sound easier?

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    that makes sense but i mean i kinda know structs but not really and i mean then what data type would my queue be if i used structs? Would i then be able to access each part of the struct individually.In hk_mp5kpdw's example I would only need to create a tmp of the type customer so that i can read the list of names and data into the queue, then when i want to access them like when the customer goes on the ride take 1 ticket away i would do tmp.num_tickets-1? but wouldn't everything in the queue be tmp.variable name? The way the program will work as I have planned is this. There is one queue called arrivals and simply the infile will read the data into the queue, then at the correct time all the people with that time will move into another queue named line at which point 1 ticket will be taken away. Time will be incremented 1 and the first person on the line queue will be checked to see if he has another ticket, if not the last name and time value will be written to the out file, if he does, then all the people from the arrivals queue with the new time will be put into line, then the person that had a ticket left will go at the end of this line, and the program will go until both queues are empty. Only one person can ride the ride at a time. Thats what i have planned to do but whats holding me up is how i am going to store a string, and two ints as a package into a queue and access the ints and change them.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    struct Rider
    {
        std::string name;
        int tickets, arrival;
    };
    
    std::queue<Rider> myQueue;
    
    //goes on a ride:
    Rider onTheRide = myQueue.front();
    myQueue.pop();
    onTheRide.tickets--;
    
    //if tickets left, back in the queue
    myQueue.push(onTheRide);
    Something on the line.
    At first you'll probably need to put the Riders in a vector or list and sort them according to the arrival time.
    Then set up a loop, representing each unit of time.
    Check if someone should be added to the queue this time.
    Check if there is someone who has been riding and add him to the queue if he has tickets left.
    If he doesn't have any tickets, print that he has finished.
    Pop the first one in the queue, if any, to a ride.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    Am i just putting the struct part in my main.cpp before the int main() part? BTW thanks for the code as an example i'm not doing it that way really but can use some of the code like Rider onTheRide so thank you for the example to help me see easier what you mean.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, the struct goes before the main.

    By the way, your expected input seems to be wrong. Here's a list of who comes off a ride at a given time and how many tickets they have left:
    Code:
    1: END, tickets: 0
    2: END, tickets: 0
    3: Hopper, tickets: 0
    4: Torvalds, tickets: 9
    5: Torvalds, tickets: 8
    6: Torvalds, tickets: 7
    7: Torvalds, tickets: 6
    8: Torvalds, tickets: 5
    9: Jobs, tickets: 3
    10: Torvalds, tickets: 4
    11: Jobs, tickets: 2
    12: Torvalds, tickets: 3
    13: Jobs, tickets: 1
    14: Torvalds, tickets: 2
    15: Jobs, tickets: 0
    16: Torvalds, tickets: 1
    17: Torvalds, tickets: 0

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    ok i coded some and where i'm at is copying the customers with the same time arrival as that of the clock into the line queue. heres my code any suggestions would be helpful.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    //this is a struct named Rider that will be used to store all the data of a
    //rider on the fair ride, this makes it easier so that I can make a queue of
    //type rider instead of multiple queues for each data type of data
    struct rider
    {
        //variable for last name of rider
        string lastName;
    
        //variable for number of tickets the rider has
        int tickets;
    
        //variable for the arrival time of the rider
        int arrival;
    };
    
    int main()
    {
        //variable used to count the ticket
        int tickets;
    
        //variable used to keep time
        int clock;
    
        //tmp variable used in loops as counter
        int i;
    
        //queue that the information from the infile gets stored into 
        queue<rider> arrivals;
    
        //queue that the riders with the correct clock time get put into
        queue<rider> line;
    
        //string that makes it easier to remember the name of the file to open
        //to read from
        string  inFileName = "riders.dat";
    
        //string that makes it easier to remember the name of the file to open
        //to write to
        string  outFileName = "riders.out";
       
        //file read from
        ifstream inFile;
    
        //file write to
        ofstream outFile;
        
        //opens the file to read from
        inFile.open(inFileName.c_str());
    
        //opens the file to write to
        outFile.open(outFileName.c_str());
    
        //while the infile can be read from
        while(inFile)
        {
        
        //creates customer of the struct rider;
        rider customer;
    
        //it will loop and read from the inFile data while the customer data
        //is not equal to END 0 0 
        while(lastName != 'END' && tickets != 0 && arrivals != 0)
        {
            //here the file will place the correct data for each customer into 
            //the correct variables
            inFile >> customer.lastName >> customer.tickets >> customer.arrivals;
    	
            //after the customer in that particular read is done putting it in the
            //correct variables the customer gets pushed ito arrivals queue
            arrivals.push(customer);
        }
    
        //once all the customers are read from the inFile and put into the arrivals
        //queue the program and clock will have to be run where this data will be 
        //processed. For the processing part of the program, it will run while
        //the queues arrivals and line are not empty, therefore they will stop
        //running when they become empty.
        while(arrivals.empty() != true && line.empty() != true)
        {
        
    
    
        //closes the infile
        inFile.close();
    
        //closes the outfile          
        outFile.close();
    
        //signals ok to the os
        return 0;
    };

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Not bad. I wouldn't use a queue for putting the input into, because it would be better if you could sort the container.

    What I used for that was a list - as it can be sorted and it is easy to treat its beginning as a queue.
    If you use a list, you'll need to overload the < operator for Riders to be able to use the list sort method. The prototype is:
    Code:
    bool operator< (const Rider& a, const Rider& b);
    Code:
    while(arrivals.empty() != true && line.empty() != true)
    This condition may be wrong. It says to keep looping while there is something in both queues. You may have meant: keep looping while there is something in either queue.
    And even then the condition may be wrong wrong. If a person is taking a ride, he needn't be in either queue (the last person spends all the time riding).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem about stack and queue in C
    By neyul in forum C Programming
    Replies: 4
    Last Post: 04-07-2009, 10:34 PM
  2. Double Linked List Problem
    By Shiggins in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2009, 07:15 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Problem when concatenating two dynamic queues
    By difficult.name in forum C Programming
    Replies: 3
    Last Post: 10-27-2004, 12:27 AM
  5. queue problem
    By Yelagin in forum C Programming
    Replies: 1
    Last Post: 07-24-2002, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21