In: Computer Science
(c++) Create a program that is like deli. You can enter peoples names in, and they will be placed into a stl queue. You can also choose to "serve" someone, which will choose the next person in line. (It will say "now serving" and then the persons's name.). Last, you can choose to "quit", which will "serve" the remaining people in the queue, and then the program will end.
There is very little information provided in the problem statement regarding the code, I was confused a little about how the code should be, so I have coded a menu-driven program that has the option of adding people in the queue, serving and quit as mentioned in the problem statement. If you needed something else, you can mention in the comment section I will modify the code ASAP. A sample of output has been added below the code as well.
CODE:
#include<iostream>
#include<queue>
#include<string>
using namespace std;
int main(){
queue<string> deli; // created a queue of string named deli
string name; // a string variable to store the name of person
while(1){
cout<<"\n1. Enter People's Name: "<<endl; // menu for adding, serving and quitting
cout<<"2. Serve someone"<<endl;
cout<<"3. Quit "<<endl;
cout<<"\nEnter your choice? ";
int choice;
cin>>choice;
switch(choice){
case 1:
cout<<"Enter person name: ";
cin>>name;
deli.push(name); // pushed the name in the queue
cout<<"Added in queue successfully"<<endl;
break;
case 2:
name = deli.front();
deli.pop(); // popped the first name in the queue
cout<<"Serving "<<name<<"..."<<endl;
cout<<"Served successfully"<<endl;
break;
case 3:
while(!deli.empty()){ // in case of quit, pop and print all the name until queue gets empty and end the program.
cout<<"Serving "<<deli.front()<<"..."<<endl;
deli.pop();
}
cout<<"Served successfully"<<endl;
exit(0);
break;
default:
cout<<"Invalid choice! please enter again..."<<endl;
break;
}
}
return 0;
}
OUTPUT:
NOTE: IN case of any query, you are always welcome in the comment section. HAPPY LEARNING!!