How to print 2D dynamic array property in c++ function

I have a flight class which has row seat and bool** seatReserv properties

class Flight {
 public:
 int flightNumber;
 int row;
 int seat;
 bool** seatReserv;

I am trying to print out seats in each row in a function, my function aims if seat is true this means this seat is not occupied and if it is false it is occupied, for true bool seats my function must print "O" and for false bool it must print "X"
Here is my function below:

void ReservationSystem::showFlight(const int flightNo){
 bool found = false;
 int index;
 for(int i = 0; i < totalFlights; i++){
        if(flightArr[i].flightNumber == flightNo){
            found = true;
            index = i;
        }

        }
 if(found){
        for(int i = 0; i < flightArr[index].row;i++){
                cout<<i<<" ";
            for(int j = 0; j < flightArr[index].seat;j++){
                if(flightArr[index].seatReserv[i][j] == true){
                    cout<< "O"<<" ";
                }
                else{
                    cout<< "X"<< " ";
          }

          }
            cout<<endl;
          }

          }
          }

My ReservationSystem class has 2 property:

class ReservationSystem {
int totalFlights;
Flight* flightArr;
};

When i test my showFlight, output was:

int main() {
ReservationSystem R;
R.showAllFlights();
R.addFlight(104,7,4);
R.showFlight(104);

return 0;
}
Output is: 0 O O O O
           1 O O O O
           2 O O O O
           3 O O O O
           4 O O O O
           5

So how can i fix this problem, when i change the default constructors row value output changes but i couldn’t understand why it is happening, here is my default constructor:

Flight::Flight(){
        flightNumber = 0;
        row = 5;
        seat = 5;
        seatReserv = new bool*[row];

        for(int i = 0; i < row;i++){
            seatReserv[i] = new bool[seat];
            for(int j = 0;j < row; j++){
                seatReserv[i][j] = true;
             }
    }
    }

so the problem is, i can not print out more values then default constructors row and seat value. For example if my default constructor’s row = 5 and seat = 4, and if i add a flight which has 8 rows and 5 seats in each row, i can not print more then 5 rows and 4 seats, default constructor restricts the program but i couldn’t understand why that is happening?

Source: Windows Questions C++

LEAVE A COMMENT