Problem with class/array of object

Hello, I have been having a problem with a code that involves creating a flight booking system that stores a maximum of 10 flights, with each flight containing a capacity and reserved seats. While the user doesn't input quit, the user can change the number of reserved seats, add a new flight or delete a flight. My problem is that when I try to change the number of reserved seats, it does not change even though I use a this pointer to overwrite the initially set reserved. Another problem I'm facing is the fact that I do not know how to delete a flight id.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//creating class 
class FlightBooking 
{
//creating public class members
public:
FlightBooking(int id, int capacity, int reserved);
FlightBooking();
void printStatus();
bool reserveSeats(int number_of_seats, int reserved);
bool cancelSeats(int number_of_seats, int reserved);
int getID(int id);

//creating private class members
private:
int id;
int capacity;
int reserved;
float percent;
};

//saves inputted flight information when creating a flight 
FlightBooking::FlightBooking(int id, int capacity, int reserved)
{
  this -> id = id;
  this -> capacity = capacity;
  this -> reserved = reserved;
}

FlightBooking::FlightBooking()
{ 
  id = 0; 
  capacity = 0; 
  reserved = 0;
}

//returns id value 
int FlightBooking::getID(int id)
{ 
  this -> id = id; 
  return id;
}

//function prints status of flight
void FlightBooking::printStatus()
{
  percent = (reserved * 100.0 / capacity); //calculate percent
  cout << "\nFlight " << id << ": " << reserved << "/" << capacity << " " << setprecision(4) << percent << "% seats taken\n";
}

//function reserves inputted seats to specific flight 
bool FlightBooking::reserveSeats(int number_of_seats, int reserved)
{
  //if the reserved seats + added seats are less then or equal to limit, save the added seats 
  if(((reserved + number_of_seats) * 100.0 / capacity) <= 105) //proceeds to add seats if capacity does not exceed 105%
  {
    reserved += number_of_seats;
    cout << "  " << reserved << "  "; //checking to see what value reserved gives before exiting function
    this -> reserved = reserved; 
    return true; 
  }
    cout << "You reached the 105% capacity of reserved seats on this flight";
    return false;

}

//function cancels reserved seats on a specific flight 
bool FlightBooking::cancelSeats(int number_of_seats, int reserved)
{
  //if the deleted seats doesn't = to less than 0, save the current number of seats
  if(reserved - number_of_seats >= 0)  
  {
    reserved -= number_of_seats;
    this -> reserved = reserved;
    return true;
  }
  else
  {
    cout << "Cannot perform this operation; more seats being removed than initially registered"<< endl;
  return false;
  }
}

int main() {
  int capacity, id, counter = 0;//sets variables
  int reserved = 0;
  string command;

  //creates array of objects to store flight ids
  FlightBooking booking[10]; 

  //gives user prompts and what each prompt does
  cout << "A. create \n   creates empty flight [id] with [capacity]";
  cout <<"\nB. delete \n   deletes flight [id] \n";
  cout << "C. add \n   adds [n] reserved seats to flight [id]\n";
  cout << "D. cancel \n   cancels [n] reservations to flight [id] \nE. quit \n   exits program" << endl;
  
 do //do while loop that continues until user inputs quit
 { 
   int seats = 0; 
   //prints every flight id info that has been inputted
   for(int i = 0; i < counter ; i++) 
    booking[i].printStatus();
   
   cout << "\nWhat would you like to do: "; //prompts user
   cin >> command;

   //user can create a flight until it reaches a limit of 10 ids
   if(command == "create" && counter < 10)
   {
     do{ //loop repeats if either flight number or capacity is equal to 0
       cout << "Provide flight number: ";
       cin >> id;
       cout << "Provide flight capacity: ";
       cin >> capacity; 
      }while(capacity <= 0 || id <= 0);

    booking[counter] = FlightBooking(id, capacity, reserved);
    booking[counter].getID(id);
    counter++;
    }
  
  //user is warned that it has reached the limit of creating flights
  else if(command == "create" && counter >= 10)
      cout << "You have reached the maximum capacity of 10 flights";

  //adds seats to reserved
   else if(command == "add")
   {
     cout << "Provide id number: ";
     cin >> id;
     cout << "Provide number of reserved seats: ";
     cin >> seats;
     //if the flight id is found
     if(booking[counter].getID(id) > 0) 
     {
       booking[counter] = FlightBooking(id, capacity, reserved);
       booking[counter].reserveSeats(seats, reserved) ; //flight adds reserves seats
       cout << reserved; //checking to see what value reserved gives after exiting function
     }
     else //if flight id not found, prompts user to try again
       cout << "Flight ID not found; try again";
    }

  //cancel seats to reserved
  else if(command == "cancel")
    {
      cout << "Provide id number: ";
      cin >> id;
      cout << "Provide number of cancelled seats: ";
      cin >> seats;
      //if the flight id is found
      if(booking[counter].getID(id) > 0) 
      {
        booking[counter] = FlightBooking(id, capacity, reserved);
        booking[counter].cancelSeats(seats, reserved); //flight cancels reserves seats
      }
      else //if flight id not found, prompts user to try again
       cout << "Flight ID not found; try again";
    }

    //deletes the flight id that the user had previously inputted
   else if(command == "delete")
   {
     cout << "Provide id number: ";
     cin >> id;
     //if the flight id is found
     if(booking[counter].getID(id) > 0)
     {
       booking[counter] = FlightBooking(id, capacity, reserved);
       booking[counter] = FlightBooking(); //flight is deleted by setting all of its information to 0
       counter--;
     }
     else //if flight id not found, prompts user to try again
       cout << "Flight ID not found; try again";
   }
     
  else if(command != "quit") //if the command does not match any of the options, ask to try again 
    cout << "Invalid command; try again"<< endl;
   
  }while(command != "quit");

  return 0;
}



I would appreciate it if someone could explain to me what I might be doing wrong, since I'm a bit of a newbie and haven't learned classes quite wel. Thanks!
Last edited on
percent doesn't need to be a member variable. It makes more sense as a local variable inside FlightBooking::printStatus().

In "add", "cancel" and "delete" you access booking[counter] which is one past the last booked flight. I think what you want to do is to search the array to find the flight with the id that the user specified and carry out the action on that one.
Last edited on
I would appreciate it if someone could explain to me what I might be doing wrong
I've had a close look at your code and besides the business about percent which is derived when it's needed using getPercent() you are making the job a bit complicated. The code is getting fairly convoluted. Remember that once you have declared a Flight object with its name and capacity you don't need to pass these parameters in its methods. The object 'knows' them.

First, I think you need to separate Flights from Bookings. They are better as distinct and separate objects. You have multiple Bookings on a flight, all with different numbers of seats reserved within the limits.

There are basic things/methods/properties of each these 2 classes, which, if kept separate, make your system much easier to manage.

You might decide to make a Booking class to encapsulate your array and thereby keep as most as possible out of main(). Leave main() for the menu and shunt the bulk of the work off to the classes/objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>

class Flight
{
private:
    int m_flight_no{0};
    int m_capacity{0};
    int m_reserved{0};
public:
    Flight();
    Flight(int, int);
    double getPercent();
    int getFlightNo();
    void setFlightNo(int);
    bool addReservations(int);
    bool deleteReservations(int);
    
    void print();
};

Flight::Flight(){};
Flight::Flight(int a_flight_no, int a_capacity)
{
    m_flight_no = a_flight_no;
    m_capacity = a_capacity;
}

double Flight::getPercent() { return m_reserved * 100/m_capacity;}

int Flight::getFlightNo(){ return m_flight_no;}

void Flight::setFlightNo(int a_flight_no)
{ m_flight_no = a_flight_no; m_reserved = 0; }

bool Flight::addReservations(int no_reservations)
{
    int temp = no_reservations + m_reserved;
    if(temp * 100 <= m_capacity * 105)
    {
        m_reserved += no_reservations;
        return true;
    }
    else
    {
        return false;
    }
};

bool Flight::deleteReservations(int no_reservations)
{
    if(m_reserved - no_reservations >= 0)
    {
        m_reserved -= no_reservations;
        return true;
    }
    else
    {
        return false;
    }
};

void Flight::print()
{
    std::cout
    << "Flt no.: #" << m_flight_no << ", Capacity: " << m_capacity
    << ", Reserved: " << m_reserved << ", Percent: " << getPercent() << "%\n";
}

int main()
{
    const int LIMIT{3};
    Flight booking[LIMIT]{ {123, 100}, {6789, 250}, {17, 298}};
    
    booking[2].print();
    
    booking[2].addReservations(20);
    booking[2].print();
    
    booking[1].deleteReservations(15);
    booking[1].print();
    
    booking[2].addReservations(75);
    booking[2].print();
    
    booking[2].deleteReservations(90);
    booking[2].print();
    
    booking[2].setFlightNo(0); // use later if Flight is cancelled
    booking[2].print();
    
    int bkg_no{1};
    std::cout << booking[bkg_no].getFlightNo() << '\n';
    
    std::cout << '\n';
    
    return 0;
}


Flt no.: #17, Capacity: 298, Reserved: 0, Percent: 0%
Flt no.: #17, Capacity: 298, Reserved: 20, Percent: 6%
Flt no.: #6789, Capacity: 250, Reserved: 0, Percent: 0%
Flt no.: #17, Capacity: 298, Reserved: 95, Percent: 31%
Flt no.: #17, Capacity: 298, Reserved: 5, Percent: 1%
Flt no.: #0, Capacity: 298, Reserved: 0, Percent: 0%
6789

Program ended with exit code: 0
@spinach999 - upon looking at the code, my first thought was that main() is too long and complicated. IMO each option should be it's own function. Also it's not really a good idea to have a member function param name with the same name as a member variable. It leads to confusion.

What is the purpose of L124? It returns a value which isn't used.

When you add a reserved seats to a flight, how do you find the flight? Don't you need a loop to iterate over booking to find the required flight? Same for cancel & delete?

As a first refactor, then consider something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

constexpr size_t MaxFlight {10};

//creating class
class FlightBooking {
public:
    FlightBooking(unsigned id, unsigned capacity);
    FlightBooking() = default;

    void printStatus() const;
    bool reserveSeats(unsigned reserve);
    bool cancelSeats(unsigned cancel);
    unsigned getID() const;
    void resetId() { id = 0; }

private:
    unsigned id {};
    unsigned capacity {};
    unsigned reserved {};
};

FlightBooking::FlightBooking(unsigned id_, unsigned capacity_) : id(id_), capacity(capacity_) {}

unsigned FlightBooking::getID() const { return id; }

void FlightBooking::printStatus() const {
    if (id != 0)
        cout << "Flight " << id << ": " << reserved << "/" << capacity << " " << setprecision(4) << (reserved * 100.0 / capacity) << "% seats taken\n";
}

bool FlightBooking::reserveSeats(unsigned reserve) {
    if (((reserved + reserve) * 100.0 / capacity) <= 105) { // proceeds to add seats if capacity does not exceed 105%
        reserved += reserve;
        return true;
    }

    cout << "You reached the 105% capacity of reserved seats on this flight\n";
    return false;
}

//function cancels reserved seats on a specific flight
bool FlightBooking::cancelSeats(unsigned cancel) {
    if (reserved - cancel >= 0) {
        reserved -= cancel;
        return true;
    }

    cout << "Cannot perform this operation; more seats being removed than initially registered\n";
    return false;
}

int findId(const FlightBooking booking[MaxFlight], unsigned id) {
    for (size_t i {}; i < MaxFlight; ++i) {
        if (booking[i].getID() == id)
            return i;

        if (booking[i].getID() == 0)
            return 0 - (i + 1);
    }

    return 0 - (MaxFlight + 2);
}

int getId(const FlightBooking booking[MaxFlight]) {
    int indx {};
    unsigned id {};

    do {
        cout << "Provide id number: ";
        cin >> id;
    } while (id != 0 && (indx = findId(booking, id)) < 0 && (std::cout << "Flight ID not found\n"));

    return indx;
}

void create(FlightBooking booking[MaxFlight]) {
    int indx {};
    unsigned id {}, capacity {};

    do {
        cout << "Provide id number: ";
        cin >> id;
    } while ((id == 0 || (indx = findId(booking, id)) >= 0) && (std::cout << "Invalid flight ID\n"));

    if (indx != 0 - (MaxFlight + 2)) {
        indx = -1 - indx;

        do {
            cout << "Provide flight capacity: ";
            cin >> capacity;
        } while (capacity <= 0 && (cout << "Invalid capacity\n"));

        booking[indx] = FlightBooking(id, capacity);
    } else
        cout << "You have reached the maximum capacity of " << MaxFlight << " flights\n";
}

int main() {
    string command;
    FlightBooking booking[MaxFlight] {};

    // Gives user prompts and what each prompt does
    cout << "\nA. create\tcreates empty flight [id] with [capacity]";
    cout << "\nB. delete\tdeletes flight [id]";
    cout << "\nC. add\t\tadds [n] reserved seats to flight [id]";
    cout << "\nD. cancel\tcancels [n] reservations to flight [id]";
    cout << "\nE. quit\t\texits program\n";

    do {
        int seats {};

        // Prints every flight id info that has been entered
        cout << '\n';
        for (size_t i {}; i < MaxFlight; ++i)
            booking[i].printStatus();

        cout << "\nWhat would you like to do: ";
        cin >> command;

        // User can create a flight until it reaches a limit
        if (command == "create") {
            create(booking);
            continue;
        }

        // Adds seats to reserved
        if (command == "add") {
            const int indx {getId(booking)};

            do {
                cout << "Provide number of reserved seats: ";
                cin >> seats;

            } while (!booking[indx].reserveSeats(seats));
            continue;
        }

        // Cancel seats to reserved
        if (command == "cancel") {
            const int indx {getId(booking)};

            do {
                cout << "Provide number of cancelled seats: ";
                cin >> seats;
            } while (!booking[indx].cancelSeats(seats));
            continue;
        }

        // Deletes the flight id that the user had previously entered
        if (command == "delete") {
            const int indx {getId(booking)};

            booking[indx].resetId();
            continue;
        }

        if (command != "quit")
            cout << "Invalid command; try again\n";
    } while (command != "quit");
}

Thank you everyone, I was able to fix my code!
Topic archived. No new replies allowed.