i am new to queue..can somebody help me on this.im stuck at selction 2,3 and 4

#include <iostream>
#include <queue> // Queue STL
#include <time.h> 

using namespace std;

int displayMenu()
{
    int menu;
    cout <<"\t\tQueue Numbering System"<< endl;
    cout <<"`````````````````````````````````"<< endl;
    cout <<"1. Generate queue number"<< endl;
    cout <<"2. Get total number of customers currently waiting"<< endl;
    cout <<"3. Display all customers in waiting list"<< endl;
    cout <<"4. Serve customer"<< endl;
    cout <<"5. Exit"<< endl;

    cout << endl << "Select menu:";
    cin >> menu;

    return menu;
}

int main ()
{
    //declaration
    queue <int> qnum;
    int selectedMenu;
    int customer = 1000;
    time_t rawtime;


    //display menu
    do{
        struct tm * timeinfo;
        char buffer [80];

        time (&rawtime);
        timeinfo = localtime (&rawtime);

        strftime (buffer,80,"Now it's %I:%M%p.",timeinfo);
        puts (buffer);

        selectedMenu=displayMenu();
        if (selectedMenu==1)
        {
            qnum.push(customer++);
            cout <<"Queue number generated: "<< customer << "at :" << endl;
            cout << endl;
        }
        else if (selectedMenu==2)
        {
            // display the size of the queue
            cout <<"Total customer in waiting list: " << endl;
        }
        else if (selectedMenu==3)
        {
            // todo- how to list all the element in queue
            cout <<"List of waiting customers :" << endl;
        }
        else if (selectedMenu==4)
        {
            //todo - display the qnumber to be server and remove the customer from queue
            // have to check if the customer is served or not
            cout <<"Serve customer :" <<endl;
        }
        else if (selectedMenu==5)
        {
            //todo - check if the queue is not empty
            // warning/prompt should the user want to proceed
            cout << "Exit program." << endl;
        }
        else
        {
            cout <<"Invalid input." << endl;
        }


    }while (selectedMenu!=5);

    cout <<"Thank you." <<endl;


    return 0;
}

Sorry. I'm not going to do your homework for you. That said, for #2 you can get the number of elements in the queue with the size() method. IE:
cout << "Total customer in waiting list: " << dec << qnum.size() << endl;
That's as far as I'm going to help you until you write the rest of the code to the best of your ability.

In any case, it isn't easy to traverse a queue. You can pop the entries out and then push them back, or push them into a temporary queue where you then pop them out and push them back into the now empty main queue. If you don't have to use a queue, then I would suggest using a vector. That is easily traversable. Ditto a deque. Go to http://www.cplusplus.com/reference/ for more information about all of this stuff.

Edited 11 Months Ago by rubberman

You could also use a list ...
All these containers mentioned so far... 'know their' size (hint for menu item 2)
And, using iterators ... you can easily traverse a list (hint re. menu item 3)
And using push_front and then pop_back ... you can easily do next menu item.

...using push_front and then pop_back ... you can easily do next menu item.

However ...
it makes clearer logic ...
to 'push_back' to add new customers to the end of 'the line' ...
and then to, some-time (asap) later ...
serve the customers from the 'front' of the line ... using pop_front.

Edited 11 Months Ago by David W

This article has been dead for over six months. Start a new discussion instead.