Airline ticket system take two

Hi guys I decided to review an old program,the only difference is that I'm running it on a different IDE maybe with a different compiler but not certain anyway after getting the output that was not expected I am confused,

I checked the other code and my copy constructor,assignment operator and < operator are all the same in both projects

yet this is the output

it seems like the assignment operator and or the overloaded = operator are not working

this is the original code and post

http://www.cplusplus.com/forum/beginner/225495/

there is literally no difference yet the outputs are worlds apart,sitting here for an hour now looking at each function trying to spot a small mistake but I just can't see anything different or come to a conclusion why the results differ so much




0Z
0Z
0Z
0Z
0Z
0Z
0Z
0Z
0Z

(and many more 0Z's)


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
  #include <iostream>
#include <vector>
#include <map>

using namespace std;


class Seat{

public:

    int number;
    char letter;
    bool taken;

    Seat(int number,char letter):
    number(number),letter(letter){

    taken = false;
    }
    Seat(){

        number = 0;
        letter = 'Z';
        taken = false;
    }

    Seat(const Seat &other){

      letter = other.letter;
      taken = other.taken;
      number = other.number;
    }

    Seat& operator =(const Seat& other){

         letter = other.letter;
         taken = other.taken;
         number = other.number;
         return *this;
    }

    bool operator<(const Seat& other)const{


      return number == other.number;
    }

};

class Airline{

  public:

   string name;
   int flightNumber;
   map<Seat,Seat> seats;

   Airline(string name,int flightNumber)
   : flightNumber(flightNumber),name(name){}

   Airline(){

   }

   void addSeats(int row,char letter){

      for(int i = 1; i < row; i++){

        for(char j = 'A'; j <= letter; j++){

            Seat seat(i,j);
            seats[Seat(i,j)] = seat;
        }
      }
   }

   void printSeats(int row,char letter){

     for(int i = 1; i < row; i++){

        for(char j = 'A'; j <= letter; j++){

            cout << seats[Seat(i,j)].number << seats[Seat(i,j)].letter << endl;
        }
      }
   }
};

int main()
{
    Airline Delta("Delta",101);
    Delta.addSeats(50,'F');
    Delta.printSeats(50,'F');

}
Last edited on
Line 43,46: You're overloading the < operator, but you're using the equality test (==).

Line 16,59: It's a really bad practice to name your arguments the same as your member variables. You can get away with that only because you have an initializer list. In the body of your function, your argument names mask the member variable names.

Line 73: I get a trap here, with seat 1B.

thanks anon that was the problem at line 43,46

line 73,how would I go about fixing line 73 not too sure what is meant by trap

thanks much appreciated

The trap I got was "invalid operator <", but that was because I had not corrected line 46.

Lines 68,80: I suspect you want <= if you want to go from 1 to row (inclusive).

You still have a problem in your < operator. You need to check both number and letter if you want your map to be ordered properly.
1
2
3
4
5
6
7
    bool operator < (const Seat& other) const
    {   if (number < other.number)
            return true;
        if (number == other.number && letter < other.letter)
            return true;
        return false;                        
    }




Thanks Anon much appreciated
Topic archived. No new replies allowed.