Mitchell
Mitchell

Reputation: 503

C++ Queue Simulation

As part of a homework assignment, I am supposed to write a program that simulates queues in a grocery store environment. The full assignment is explained on the linked page.

I have the program working when there is only one queue, and am trying to modify it to handle multiple queues, per the assignment description. However, I am getting a few errors when compiling.

I know the issue has to do with dequeueing a customer in line; I'm just not sure how to modify the program so it works with multiple queues.

Any assistance is greatly appreciated!

Error Messages:

qsim.cpp: In function 'int main()':
qsim.cpp:64: error: request for member 'empty' in 'line', which is of non-class type 'Queue [(((long unsigned int)(((long int)queuecount) - 1)) + 1u)]'
qsim.cpp:66: error: request for member 'dequeue' in 'line', which is of non-class type 'Queue [(((long unsigned int)(((long int)queuecount) - 1)) + 1u)]'

Main Program
The main program includes a class called Queue. I know the code is correct for this, since it works perfectly in a different test program.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "queue.h"
using namespace std;

int shortest_queue(Queue q[], int queuecount)
{
    int shortest = 0;
    for (int i = 1; i < queuecount; ++i)
    {
        if(q[i].size() < q[shortest].size())
            shortest = i;
    }
    return shortest;
}

int queue_total(Queue q[], int queuecount)
{
    int custcount = 0;
    for (int i = 0; i < queuecount; ++i)
        custcount += q[i].size();
    return custcount;
}

int main()
{
    int trans_time = 0;
    int count = 0;
    int entry_time;
    int wait_sum = 0;
    int wait_time = 0;
    int seed;
    int ARV_PROB;
    int MAX_TRANS_TIME;
    int DURATION;
    int queuecount;
    int shortline;
    int temp;

    cout << "Enter these parameters of the simulation:" << endl;
    cout << " The number of queue/server pairs: ";
    cin >> queuecount;
    Queue line[queuecount];
    cout << " The probability that a customer arrives in one tick (%): ";
    cin >> ARV_PROB;
    cout << " The maximum duration of a transaction in ticks: ";
    cin >> MAX_TRANS_TIME;
    cout << " The duration of the simulation in ticks: ";
    cin >> DURATION;
    cout << "Enter a random number seed: ";
    cin >> seed;
    srand(seed);

    for (int time = 0; time < DURATION; ++time)
    {
        if ( rand() % 100 < ARV_PROB )
        {
            shortline = shortest_queue(line, queuecount);
            line[shortline].enqueue(time);
        }
        if ( trans_time == 0 )
        {
            if ( !line.empty() )
            {
                entry_time = line.dequeue();
                temp = (time - entry_time);
                if(temp > wait_time)
                    wait_time = temp;
                wait_sum += (time - entry_time);
                ++count;
                trans_time = (rand() % MAX_TRANS_TIME) + 1;
            }
        }
        else
        {
            --trans_time;
        }
        cout << setw(4) << time << setw(4) << trans_time << "  " << line << endl;
    }

    cout << count << " customers waited an average of ";
    cout << wait_sum / count << " ticks." << endl;
    cout << "The longest time a customer waited was " << wait_time << " ticks." << endl;
    cout << queue_total(line, queuecount) << " customers remain in the lines." << endl;

    return 0;
}

Upvotes: 0

Views: 10835

Answers (1)

Jim Lewis
Jim Lewis

Reputation: 45045

Queue line[queuecount];

if ( !line.empty() )

line is not a Queue. It is an array of Queues, so you have to call empty() on the specific array element you want to check.

Upvotes: 2

Related Questions