mercredi 30 novembre 2016

Dynamic Allocation of Arrays

I'm trying to write a simple flight reservation system for my school assignment. I should dynamically create an array without determining a size. Since I have to keep track of the size of the array, I declared an integer variable named as count in my class. I also have a flight class that has a copy constructor and a couple of getters. Then I wrote the following method

void ReservationSystem::addFlight(const int flightNo, const int rowNo, const int seatNo) {
if (count == 0) {
    Flight *tmp = new Flight(flightNo, rowNo, seatNo);
    listOfFlights = new Flight*[count+1];
    listOfFlights[count] = tmp;
    count++;
} else {
    bool check = true;
    for (int i = 0; i < count && check; i++) {
        if (listOfFlights[i]->getFlightNo() == flightNo) {
            std::cout << "There is already a flight with that flight code" << std::endl;
            check = false;
        }
    }

    if (check) {
        Flight *tmp = new Flight(flightNo, rowNo, seatNo);
        Flight** tmparr = new Flight*[count + 1];

        for (int i = 0; i < count; i++) {
            Flight *f = new Flight(*listOfFlights[i]);
            tmparr[i] = f;
        }

        tmparr[count + 1] = tmp;

        for (int i = 0; i < count; i++) {
            delete listOfFlights[i];
        }

        delete listOfFlights;
        listOfFlights = tmparr;
        count++;
    }


}

}

I also have a showFlight(const int flightCode) method that shows the specific flight:

void ReservationSystem::showFlight(const int flightNo) {
bool check = true;
for (int i = 0; i < count; i++) {
    if (listOfFlights[i]->getFlightNo() == flightNo) {
        std::cout << "Flight " << listOfFlights[i]->getFlightNo() << " has " << listOfFlights[i]->getAvailableSeats()  << " available seats" << std::endl;
        listOfFlights[i]->printSeats();
        check = false;
    }
}

}

This is my default constructor and copy constructor for the Flight class:

Flight::Flight(const int flightNo, const int rowNo, const int seatNo) {
flight = flightNo;
row = rowNo;
seat = seatNo;
available = rowNo * seatNo;
flightPlan = new char*[seatNo];

// initialize the flight plan to all seats available
for(int i = 0; i < seatNo; ++i) flightPlan[i] = new char[rowNo];

for(int i = 0; i < seatNo; ++i) {
    for(int j = 0; j < rowNo; ++j) flightPlan[i][j] = 'o';
}

}

Flight::Flight(const Flight &obj) {
    const int flight = obj.flight;
    const int row = obj.row;
    const int available = obj.available;
    char** flightPlan = obj.flightPlan;

}

But in the line if (listOfFlights[i]->getFlightNo() == flightNo) xcode gives me EXC_BAD_ACCESS error. I think the reason behind this is a malfunction in my addFlight method, because since there is no objects the array points to something null, right? And since it can't reach to getFlightNo() method, it throws this error.

Notice that this is my first time with C++, so I'm a complete n00b and I can be terribly mistaken. Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire