-1

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;
}
LG1
  • 1
  • You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik May 05 '22 at 12:27
  • 3
    `cashiers->displayCashiers()` is the same as `(*cashiers).displayCashiers()` which is the same as `cashiers[0].displayCashiers()`. – Some programmer dude May 05 '22 at 12:28
  • The problem with e.g. `getShortestLine` is that it returns a *copy* of a `Queue`. All modifications you do with this copy will be lost when the copy is destructed (which happens immediately after `cashiers[0].getShortestLine().insert(star)`) – Some programmer dude May 05 '22 at 12:30
  • On an unrelated note about your design, it doesn't really make a lot of sense. Is a "cashier" really a queue in itself? Or does a "cashier" *have* a queue? Perhaps you should divide it into two classes: `Cachier` and `Queue`? – Some programmer dude May 05 '22 at 12:32
  • @SamVarshavchik I haven't tried this yet. Thanks for the suggestion - student & beginner here. I am setting this up now and will walk through my program using my debugger. – LG1 May 05 '22 at 12:42
  • @Someprogrammerdude, thanks for your response! Is it possible to modify the actual queue that is identified as having the shortest line? – LG1 May 05 '22 at 12:44
  • 2
    Oh, and I don't believe I missed this: `this[0]`?! That's really bad! Where and whom have taught you to use `->` with arrays and `this[0]`? You should stay away from that source or person, and not let them teach you anything else. Invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn nice "proper" C++. And spend more time with your requirements, analysis and design. – Some programmer dude May 05 '22 at 12:49

0 Answers0