-3

I'm trying to write a program which simulates airport operation using a deque and keep running into a "deque iterator not dereferencable error. After searching the error it says this is usually caused by trying to pop from an empty queue, but I have a check for that in my code. I can't figure out what the problem is. Any suggestions?

runner:

#include "airport.h"

using namespace std;

void menu();

bool badInput = false;
airport myAirport;

int main()
{
int numRunways;
int landingFreq;
int takeoffFreq;
int clockTime;

cout << "Welcome to CS1337 International Airport." << endl << "How many runways would you like to open today?" << endl;
do
{
    badInput = false;

    cin >> numRunways; //get input

    //if block steps through if non numerical input was entered
    if (cin.fail())
    {
        badInput = true; //flag bad input (non numerical value entered)
        //clear input stream
        cin.clear();
        cin.ignore(1000, '\n');
        //reprompt user
        cout << "Please enter only integer values." << endl;
    }
} while (badInput); //loop if bad input is entered

cout << endl << "How often do airplanes land at the airport? (1 to 100)" << endl;
do
{
    badInput = false;

    cin >> landingFreq; //get input

    //if block steps through if non numerical input was entered
    if (cin.fail())
    {
        badInput = true; //flag bad input (non numerical value entered)
        //clear input stream
        cin.clear();
        cin.ignore(1000, '\n');
        //reprompt user
        cout << "Please enter only integer values." << endl;
    }

    if (landingFreq > 100 || landingFreq < 1)
    {
        badInput = true;
        cout << "Please enter an integer between 1 and 100." << endl;
    }
} while (badInput); //loop if bad input is entered

cout << endl << "How often do airplanes takeoff from the airport? (1 to 100)" << endl;
do
{
    badInput = false;

    cin >> takeoffFreq; //get input

    //if block steps through if non numerical input was entered
    if (cin.fail())
    {
        badInput = true; //flag bad input (non numerical value entered)
        //clear input stream
        cin.clear();
        cin.ignore(1000, '\n');
        //reprompt user
        cout << "Please enter only integer values." << endl;
    }

    if (takeoffFreq > 100 || takeoffFreq < 1)
    {
        badInput = true;
        cout << "Please enter an integer between 1 and 100." << endl ;
    }
} while (badInput); //loop if bad input is entered

cout << endl << "How many minutes should the program wait between updates? (1 to 10)" << endl;
do
{
    badInput = false;

    cin >> clockTime; //get input

    //if block steps through if non numerical input was entered
    if (cin.fail())
    {
        badInput = true; //flag bad input (non numerical value entered)
        //clear input stream
        cin.clear();
        cin.ignore(1000, '\n');
        //reprompt user
        cout << "Please enter only integer values." << endl;
    }

    if (clockTime > 100 || clockTime < 1)
    {
        badInput = true;
        cout << "Please enter an integer between 1 and 10." << endl;
    }
} while (badInput); //loop if bad input is entered

myAirport = airport(numRunways, landingFreq, takeoffFreq, clockTime);

menu();

return 0;
}

void menu()
{
int inputSelect;
bool badInput;

cout << "What would you like to do?" << endl;
cout << "1. Tick the clock." << endl;
cout << "2. Get runway status." << endl;
cout << "3. Get airport statistics." << endl;
cout << "4. Exit program." << endl;

do
{
    cin >> inputSelect;
    badInput = false;

    cin >> inputSelect; //get input

    //if block steps through if non numerical input was entered
    if (cin.fail())
    {
        badInput = true; //flag bad input (non numerical value entered)
        //clear input stream
        cin.clear();
        cin.ignore(1000, '\n');
        //reprompt user
        cout << "Please enter only integer values." << endl;
    }

    if (inputSelect > 4 || inputSelect < 1)
    {
        badInput = true;
        cout << "Please enter an integer between 1 and 10." << endl;
    }
} while (badInput); //loop if bad input is entered

switch (inputSelect)
{
case 1: 
    myAirport.tick();
    break;

case 2:
    for (int i = 0; i < myAirport.numRunways(); i++)
    {
        cout << myAirport.runwayStatus(i);
    }
    break;

//case 3:

case 4:
    exit(0);
    break;
}

menu();
}

airport.cpp:

    #include "airport.h"


airport::airport(int numRunways, int aLandingFreq, int aTakeoffFreq, int aClockTime)
{
    landingFreq = aLandingFreq;
    takeoffFreq = aTakeoffFreq;
    clockTime = aClockTime;

    for (int i = 0; i < numRunways; i++)
    {
        myRunways.push_back(runway());
    }
}

airport::airport()
{
    landingFreq = 0;
    takeoffFreq = 0;
    clockTime = 0;
    myRunways.push_back(runway());
}

int airport::numRunways()
{
    return myRunways.size();
}

string airport::runwayStatus(int runwayNumber)
{
    return
        "Runway number " + to_string(runwayNumber) + " currently has " +
        to_string(myRunways[runwayNumber].getTakeoffPlanes()) + " planes waiting to takeoff and " +
        to_string(myRunways[runwayNumber].getlandingPlanes()) + " planes waiting to land. The total wait time is " +
        to_string(myRunways[runwayNumber].getTotalWaitTime()) + " minutes. The waiting time to land is " +
        to_string(myRunways[runwayNumber].getLandingWaitTime()) + " minutes.";
}

void airport::tick()
{
    int currentClock = clockTime;

    int landingProb = rand() % 100;
    int takeoffProb = rand() % 100;

    if (landingProb < landingFreq)
        landingArrive();
    if (takeoffProb < takeoffFreq)
        takeoffArrive();

    for (int i = 0; i < myRunways.size(); i++)
    {
        do
        {
            if (myRunways[i].getlandingPlanes())
            {
                if (myRunways[i].getNextPlane("land").getTime() < currentClock)
                {
                    currentClock = currentClock - myRunways[i].getNextPlane("land").getTime();
                    myRunways[i].removePlane("land");
                    cout << "A plane has landed on runway " << i << "." << endl;
                }
                else
                    myRunways[i].getNextPlane("land").setTime(myRunways[i].getNextPlane("land").getTime() - currentClock);
            }

            else if (myRunways[i].getTakeoffPlanes())
            {
                if (myRunways[i].getNextPlane("takeoff").getTime() < currentClock)
                {
                    currentClock = currentClock - myRunways[i].getNextPlane("takeoff").getTime();
                    myRunways[i].removePlane("takeoff");
                    cout << "A plane has taken off from runway " << i << "." << endl;
                }
                else
                    myRunways[i].getNextPlane("land").setTime(myRunways[i].getNextPlane("land").getTime() - currentClock);
            }
        } while (currentClock > 0);
    }

    for (int i = 0; i < myRunways.size(); i++)
    {
        myRunways[i].addTimeInQueue(clockTime);
    }
}

void airport::landingArrive()
{
    int minTime = 0;
    int minID = 0;

    int time = 0;
    bool badInput;

    cout << "A new plane has arrived to land. How long does this plane take to land?" << endl;

    do
    {
        badInput = false;

        cin >> time; //get input

        //if block steps through if non numerical input was entered
        if (cin.fail())
        {
            badInput = true; //flag bad input (non numerical value entered)
            //clear input stream
            cin.clear();
            cin.ignore(1000, '\n');
            //reprompt user
            cout << "Please enter only integer values." << endl;
        }
    } while (badInput); //loop if bad input is entered

    for (int i = 0; i < myRunways.size(); i++)
    {
        if (myRunways[i].getLandingWaitTime() <= minTime)
            minID = i;
    }

    myRunways[minID].addPlane(airplane("land", time));
}

void airport::takeoffArrive()
{
    int minTime = 0;
    int minID = 0;

    int time = 0;
    bool badInput = false;

    cout << "A new plane has arrived to takeoff. How long does this plane take to takeoff?" << endl;

    do
    {
        cin >> time; //get input

        //if block steps through if non numerical input was entered
        if (cin.fail())
        {
            badInput = true; //flag bad input (non numerical value entered)
            //clear input stream
            cin.clear();
            cin.ignore(1000, '\n');
            //reprompt user
            cout << "Please enter only integer values." << endl;
        }
    } while (badInput); //loop if bad input is entered

    for (int i = 0; i < myRunways.size(); i++)
    {
        if (myRunways[i].getTotalWaitTime() <= minTime)
            minID = i;
    }

    myRunways[minID].addPlane(airplane("takeoff", time));
}



airport::~airport()
{
}

runway.cpp:

#include "runway.h"


runway::runway()
{
    totalWaitTime = 0;
    landingWaitTime = 0;
}

int runway::getLandingWaitTime()
{
    return landingWaitTime;
}

int runway::getTotalWaitTime()
{
    return totalWaitTime;
}

void runway::addPlane(airplane aPlane)
{
    string type = aPlane.getType();

    if (type == "land")
    {
        landings.push_back(aPlane);
        landingWaitTime = landingWaitTime + aPlane.getTime();
    }
    else 
        takeoffs.push_back(aPlane);

    totalWaitTime = totalWaitTime + aPlane.getTime();
}

void runway::removePlane(string type)
{
    airplane removePlane;

    if (type == "land")
    {
        removePlane = landings.front();
        landingWaitTime = landingWaitTime - removePlane.getTime();
        landings.pop_front();
    }
    else
    {
        removePlane = takeoffs.front();
        takeoffs.pop_front();
    }

    totalWaitTime = totalWaitTime - removePlane.getTime();
}

airplane runway::getNextPlane(string type)
{
    //if (!landings.empty())
    //{
        if (type == "land")
            return landings.front();
        else
            return takeoffs.front();
    //}

    //else return 
}

bool runway::getlandingPlanes()
{
    return landings.empty();
}

bool runway::getTakeoffPlanes()
{
    return takeoffs.empty();
}

void runway::addTimeInQueue(int aTime)
{
    for (int i = 0; i < landings.size(); i++)
    {
        landings[i].addTimeInQueue(aTime);
    }

    for (int i = 0; i < takeoffs.size(); i++)
    {
        takeoffs[i].addTimeInQueue(aTime);
    }
}



runway::~runway()
{
}

airplane.cpp:

#include "airplane.h"


airplane::airplane(string aType, int aTime)
{
    type = aType;
    time = aTime;
}

airplane::airplane()
{
    type = "takeoff";
    time = 0;
}

void airplane::setType(string aType)
{
    type = aType;
}

void airplane::setTime(int aTime)
{
    time = aTime;
}

void airplane::addTimeInQueue(int aTimeInQueue)
{
    timeInQueue = timeInQueue + aTimeInQueue;
}

string airplane::getType()
{
    return type;
}

int airplane::getTime()
{
    return time;
}

int airplane::getTimeInQueue()
{
    return timeInQueue;
}

airplane::~airplane()
{
}

1 Answers1

2
bool runway::getlandingPlanes()
{
    return landings.empty();
}

That returns true if there are no landing planes. You're interpreting the result as indicating that there are some landing planes (as the function name implies). You should change the sense of the return value:

return !landings.empty();
       ^

The same applies to getTakeoffPlanes

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644