I'm working on a project that simulates a line at a grocery store. There are several cashiers and I need the program to determine which cashier has the shortest line and insert an asterisk representing a customer into that cashier's queue. Right now I have my individual cashier queues in an array called "cashiers". What I'm currently stuck on is being able to insert to my individual cashier's queues. By calling: cashiers->getShortestLine() I am able to get the cashier's name, but unable to call a successful insert. I'm not getting an error message; the insert call doesn't appear to be working (the cashier with the shortest queue isn't receiving another asterisk-customer). I'll post my code below. In this example, cashier Burt has the shortest line, but getting the program to automatically insert a new "customer" to the shortest line does not seem to be working.
Let me know if I can provide more info and thanks so much for any help.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Queue{
private:
int maxSize;
vector<char> vecQueue;
string name;
char front;
char rear;
int nItems;
public:
Queue(int max, string n) : maxSize(max), name(n), front(0), rear(-1), nItems(0){
vecQueue.resize(maxSize);
}
void insert(char c){
if(rear == maxSize-1){
rear = -1;
}
vecQueue[++rear] = c;
nItems++;
}
char remove(){ //take item from front of queue
char temp = vecQueue[front++]; //get value and incr front
if(front == maxSize) //deal with wraparound
front = 0;
nItems--; //one less item
return temp;
}
bool isEmpty(){
return (nItems == 0);
}
int numItems(){
return nItems;
}
string getName(){
return name;
}
Queue getShortestLine(){
Queue smallest = this[0];
for(int i = 0; i < 9; i++){
if(smallest.numItems() > this[i].numItems()){
smallest = this[i];
}
}
cout << "Shortest Line: " << smallest.getName() << endl;
return smallest;
}
Queue getLongestLine(){
Queue longest = this[0];
for(int i = 0; i < 9; i++){
if(longest.numItems() < this[i].numItems()){
longest = this[i];
}
}
cout << "Longest Line: " << longest.getName() << endl;
return longest;
}
void displayCashiers(){
for(int i = 0; i < 9; i++){
cout << this[i].getName() << ": ";
for(int j=0;j<this[i].numItems();j++){cout << "*";}
cout << endl;
}
cout << endl;
}
};
int main(){
char star = '*';
Queue cashierAbigail(100, "Abigail");
Queue cashierBurt(100, "Burt");
Queue cashierCaitlin(100, "Caitlin");
Queue cashierDylan(100, "Dylan");
Queue cashierEmily(100, "Emily");
Queue cashierFrank(100, "Frank");
Queue cashierGerald(100, "Gerald");
Queue cashierHeather(100, "Heather");
Queue cashierIsaac(100, "Isaac");
//Cashier Abigail
cashierAbigail.insert(star);
cashierAbigail.insert(star);
cashierAbigail.insert(star);
//Cashier Burt
cashierBurt.insert(star);
cashierBurt.insert(star);
//Cashier Caitlin
cashierCaitlin.insert(star);
cashierCaitlin.insert(star);
cashierCaitlin.insert(star);
//Cashier Dylan
cashierDylan.insert(star);
cashierDylan.insert(star);
cashierDylan.insert(star);
//Cashier Emily
cashierEmily.insert(star);
cashierEmily.insert(star);
cashierEmily.insert(star);
//Cashier Frank
cashierFrank.insert(star);
cashierFrank.insert(star);
cashierFrank.insert(star);
//Cashier Gerald
cashierGerald.insert(star);
cashierGerald.insert(star);
cashierGerald.insert(star);
cashierGerald.insert(star);
//Cashier Heather
cashierHeather.insert(star);
cashierHeather.insert(star);
cashierHeather.insert(star);
//Cashier Isaac
cashierIsaac.insert(star);
cashierIsaac.insert(star);
cashierIsaac.insert(star);
Queue cashiers[9] = {
cashierAbigail,
cashierBurt,
cashierCaitlin,
cashierDylan,
cashierEmily,
cashierFrank,
cashierGerald,
cashierHeather,
cashierIsaac
};
cashiers->displayCashiers();
cashiers->getShortestLine().insert(star);
cashiers->displayCashiers();
return 0;
}