in my C++ class we were given an assignment (as follows) and i only have one last objective with it. the user is given an option to be able to see the entire queue list and i don't know how to get the program to show that. does anyone know how this can be done? my problem starts at about line 42 with if(iChoice == 3). here's what i have so far:

#include <iostream>
#include <list>
#include <queue>
#include <string>
using namespace std;

int main()
{
queue<string> myqueue;
string strName;
char cContinue;
char cAccess;
int iChoice;


while(1)
{
cout << "Hello! Welcome to Dr.Smith's office! \n" << endl;
cout << "To continue, press any key" << endl;
cin >> cContinue;
cout << "If you would like to put your name on the waiting list, please enter '1'" << endl;
cout << "If you would like to see who is next in line, please enter '2'" << endl;
cout << "If you would like to access the entire queue, please enter '3'" << endl;
cin >> iChoice;
if(iChoice == 1)
{
cout << "Please enter your name" << endl;
cin >> strName;
cout << "Thank you " << strName << "! Please be seated and the doctor will see you momentarily." << endl;
myqueue.push(strName);
}
if(iChoice == 2)
{
cout << "The Next person in line is ";
cout << "" << myqueue.front() << endl;
myqueue.pop();
if(myqueue.empty())
{
cout << "There is no one in the queue." << endl;
}
}
if(iChoice == 3)
{
for(int x=0; x < myqueue.size();x--)
{

cout << "These are the people currently waiting: \n" << endl;

cout << "" << << endl;
}
if(myqueue.empty())
{
cout << "There is no one in the queue." << endl;
}
}


}
return 0;
}

I don't know if this was the intention, but perhaps make a copy of the queue and pop all of the elements off? (no warranty granted on this one lol)

Edited 5 Years Ago by jonsca: n/a

what do you mean by that? and if the user chooses option 3 i need the program to show the whole queue

If you pop all of the elements off of your existing queue to view them, then there will be an empty queue. I was presuming you wanted to be able to view the copy of it while leaving the original intact.

well i need to be able to show the entire queue at anytime while being able to add names at anytime. and cplusplus.com has helped me none and i have to have this turned in in 6 hours. someone said i should run it through a loop where it would display it one by one but there has to be an easier way to do that

Somehow, this got posted before I was done. I've removed that first post and added this one.

on line 44 why are you decrementing x?
why is line 47 inside the for loop? std::queue is a container adaptor that among other things prevents access to the elements (you can only see the front and back elements). Thus, to show the contents of the queue, you either have to keep track separately (in which case why do you need the queue in the first place?) or you have to do something like:

newqueue = queue<string>;
while(! myqueue.empty()) {
  item = myqueue.front();
  myqueue.pop();
  cout << item << ", ";
  newqueue.push(item);

}
myqueue = newqueue; // beware of memory leaks!

Edited 5 Years Ago by griswolf: n/a

i decremnted x because before it would show that statement twice when i did x++. now it only shows it once. and let me try that.

That's the catch-22, you can't view all the members and keep them in their place in the queue (as griswolf pointed out). That's the crux of the datastructure. I like griswolf's idea, or along those same lines, pop them out and push them right back onto the queue until the first element is first in line again.

well why im trying that my teachers here's my teachers actual instructions: Create a queue program to manage people in a waiting room to see the Doctor Smith. Create a loop that asks the person which of 3 options they want to do.

1. Enter new name at end of queue. (Get string from user and push() to back of queue.)

2. Get the next person to serve. (Show next in line with front(), pop() from front of queue.)

3. List people waiting to be served. (Copy queue to another name, show next, pop.)

if this heps at all well here you go. if not im sorry. id also like to apologize if i seem a little curt. ive been working on this one problem since noon

(Copy queue to another name, show next, pop.)

That's essentially what I was saying to do. I'm unsure of what your hesitation is.
(I forgot that pop() doesn't return the value of the popped object, so showing then popping is the way to go, sorry)

id also like to apologize if i seem a little curt.

No worries.

Just like when you declare an object and copy in another.

Queue<string> copyq(myqueue);

or call it Bob or something.

Edited 5 Years Ago by jonsca: n/a

Based on your original post and griswolf's explanation you should know how to show all of the members. Give it a try first, knowing that copyq is an exact copy of your original queue.

ah sweeeet. the only thing im stuck on is i dont understand what griswolf is refering to with his "item"

That is the element at the front of the queue. Each item an element is popped and pushed into another queue. You get all the elements of the queue one by one. The item is an element of the queue.

Oh, it's just a string to temporarily hold the string at the front of the queue, since he's putting the elements into a new queue he created (which was not a copy, but just an empty queue) -- once the first element is popped off of the original queue, it is lost.

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