Airline ticket system

Hi guys I am making an airline ticket system and have run into a snag I have been been looking at this code for an hour now and I can't see anything wrong with it but the input i horribly wrong instead of printing the seat numbers from lets say 0 to 55 and A to T instead all seats print 0Z which is the default but I overloaded the = assignment and copy constructor,I do know that when adding to a map the map creates a new object with the default constructor then uses the assignment operator to copy all the info into the new object which is in the map BUT for some reason all the numbers and letters are 0 and Z I tried debugging by putting in some code to see if the = and copy constructor runs and both of them run yet for some reason it's not printing the values I expected (or assigning)


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
  #include "Airline.h"

Airline::Airline() {
	// TODO Auto-generated constructor stub

}

Airline::Airline(string flightName, int flightNumber) :
		flightName(flightName), flightNumber(flightNumber) {

}

Airline::Airline(const Airline& other) {

	flightName = other.flightName;
	flightNumber = other.flightNumber;

}

Airline& Airline::operator=(const Airline& other) {

	flightName = other.flightName;
	flightNumber = other.flightNumber;

	return *this;
}

void Airline::addSeats(int row, char seatLetter) {

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

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

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

void Airline:: printSeats(int row,char seatLetter){

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

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

			cout << seats[Seat(i, j)].row
					<< seats[Seat(i, j)].seatLetter << endl;

			}
		}
}

Seat Airline:: findSeat(int row,char seatLetter){



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

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

             if(i == row && j == seatLetter){

                return seats[Seat(row,seatLetter)];

             }
     }
}
}

void Airline:: bookSeat(int row,char seatLetter){

         seats[Seat(row,seatLetter)].taken = true;
}

Airline::~Airline() {

}





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

#include "Seat.h"

Seat::Seat() {

    row = 0;
    seatLetter = 'Z';
    taken = false;

}

Seat::Seat(int row,char seatLetter)
: row(row),seatLetter(seatLetter)
{

    taken = false;
}

Seat::Seat(const Seat& other){

	row = other.row;
	seatLetter = other.seatLetter;
	taken = other.taken;

}

Seat& Seat:: operator=(const Seat& other){

	row = other.row;
	seatLetter = other.seatLetter;
	taken = other.taken;

	return *this;
}

bool Seat:: operator<(const Seat& other)const{

      if(row == other.row){

    	  return seatLetter == other.seatLetter;
      }
}

Seat::~Seat() {

}
And how are you actually setting values in the Seat object?


And how are you actually setting values in the Seat object?



1
2
3
4
5
6
7
8
9
10
11
12
13

void Airline::addSeats(int row, char seatLetter) {

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

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

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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "Airline.h"

#include "Seat.h"

using namespace std;

int main() {
	Airline airline("Delta",12345);
	airline.addSeats(50,'F');

	airline.printSeats(50,'F');
	airline.findSeat(40,'D');

}



I have singled it out it's coming from the opeartor< function I need to find a way of checking if one is less than the other


I was hoping this would have worked but to no avail


1
2
3
4
5
6

bool Seat:: operator<(const Seat& other)const{

      return row < other.row && seatLetter < other.seatLetter ;
}
Last edited on
seats[Seat(i,j)] = seat;

You're making a map<Seat, Seat> ? That seems very odd.

Anyway, your comparison function puts seat 3A before 4B, but seat 4A before 3B. That seems wrong. I suspect you might mean something like this.

1
2
3
4
5
6
bool Seat:: operator<(const Seat& other)const{
    if (row != other.row) // if different rows
{ return row < other.row) // .. then compare by row
  else // if same row
  { return seatLetter < other.seatLetter;}  // ... compare by seat letter
}
Last edited on
Hints:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Airline.h"
#include "Seat.h"

int main()
{
    Airline airline("Delta", 12345);
    airline.addSeats(50, 'F');

    airline.printSeats(50, 'F');
    airline.findSeat(40, 'D');
}


Seat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef SEAT_H
#define SEAT_H

class Seat {
public:
    int row;
    char seatLetter;
    bool taken;

    Seat();
    Seat(int row, char seatLetter);
    Seat(const Seat& other);
    Seat& operator=(const Seat& other);
    bool operator<(const Seat& other) const;
    ~Seat();
};

#endif // SEAT_H 


Seat.cpp
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
#include "Seat.h"

Seat::Seat()
{
    row = 0;
    seatLetter = 'Z';
    taken = false;
}

Seat::Seat(int row, char seatLetter) : row(row), seatLetter(seatLetter)
    { taken = false; }

Seat::Seat(const Seat& other)
{
    row = other.row;
    seatLetter = other.seatLetter;
    taken = other.taken;
}

Seat& Seat::operator=(const Seat& other)
{
    row = other.row;
    seatLetter = other.seatLetter;
    taken = other.taken;
    return *this;
}

bool Seat:: operator<(const Seat& other)const{
    if (row != other.row) { return row < other.row; }
    else                  { return seatLetter < other.seatLetter; }
}

Seat::~Seat() {}


Airline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef AIRLINE_H
#define AIRLINE_H

#include "Seat.h"
#include <string>

class Airline {
public:
    Airline();
    Airline(std::string flightName, int flightNumber);
    Airline(const Airline& other);
    Airline& operator=(const Airline& other);
    void addSeats(int row, char seatLetter);
    void printSeats(int row,char seatLetter);
    Seat findSeat(int row,char seatLetter);
    void bookSeat(int row,char seatLetter);
    ~Airline();
private:
    std::string flightName;
    int flightNumber;
    Seat seats[100][26] {}; // possible overflow; use std::vector
};

#endif // AIRLINE_H 


Airline.cpp
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
#include "Airline.h"
#include <iostream>
#include <string>

// TODO Auto-generated constructor stub
Airline::Airline() {}

Airline::Airline(std::string flightName, int flightNumber)
    : flightName(flightName), flightNumber(flightNumber)
{}

Airline::Airline(const Airline& other)
{
    flightName = other.flightName;
    flightNumber = other.flightNumber;
}

Airline& Airline::operator=(const Airline& other)
{
    flightName = other.flightName;
    flightNumber = other.flightNumber;
    return *this;
}

void Airline::addSeats(int row, char seatLetter)
{
    for (int i = 1; i <= row; i++) {
        for (char j = 'A'; j <= seatLetter; j++) {
            Seat seat(i,j);
            seats[i][static_cast<int>(j)] = seat;
        }
    }
}

void Airline::printSeats(int row, char seatLetter)
{
    for (int i = 1; i < row; i++) {
        for (char j = 'A'; j <= seatLetter; j++) {
            std::cout << seats[i][static_cast<int>(j)].row
                       << seats[i][static_cast<int>(j)].seatLetter << ' ';
        }
        std::cout << '\n';
    }
}

Seat Airline::findSeat(int row, char seatLetter)
{
    for(int i = 0; i < row; i++) {
        for(char j = 'A'; j < seatLetter; j++) {
            if(i == row && j == seatLetter) {
                return seats[row][static_cast<int>(seatLetter)];
            }
        }
    }
    // should return an error value if not found
}

void Airline::bookSeat(int row,char seatLetter)
{
    seats[row][static_cast<int>(seatLetter)].taken = true;
}

Airline::~Airline() {}

Output:
1A 1B 1C 1D 1E 1F
2A 2B 2C 2D 2E 2F
3A 3B 3C 3D 3E 3F
4A 4B 4C 4D 4E 4F
5A 5B 5C 5D 5E 5F
6A 6B 6C 6D 6E 6F
7A 7B 7C 7D 7E 7F
8A 8B 8C 8D 8E 8F
9A 9B 9C 9D 9E 9F
10A 10B 10C 10D 10E 10F
11A 11B 11C 11D 11E 11F
12A 12B 12C 12D 12E 12F
13A 13B 13C 13D 13E 13F
14A 14B 14C 14D 14E 14F
15A 15B 15C 15D 15E 15F
16A 16B 16C 16D 16E 16F
17A 17B 17C 17D 17E 17F
18A 18B 18C 18D 18E 18F
19A 19B 19C 19D 19E 19F
20A 20B 20C 20D 20E 20F
21A 21B 21C 21D 21E 21F
22A 22B 22C 22D 22E 22F
23A 23B 23C 23D 23E 23F
24A 24B 24C 24D 24E 24F
25A 25B 25C 25D 25E 25F
26A 26B 26C 26D 26E 26F
27A 27B 27C 27D 27E 27F
28A 28B 28C 28D 28E 28F
29A 29B 29C 29D 29E 29F
30A 30B 30C 30D 30E 30F
31A 31B 31C 31D 31E 31F
32A 32B 32C 32D 32E 32F
33A 33B 33C 33D 33E 33F
34A 34B 34C 34D 34E 34F
35A 35B 35C 35D 35E 35F
36A 36B 36C 36D 36E 36F
37A 37B 37C 37D 37E 37F
38A 38B 38C 38D 38E 38F
39A 39B 39C 39D 39E 39F
40A 40B 40C 40D 40E 40F
41A 41B 41C 41D 41E 41F
42A 42B 42C 42D 42E 42F
43A 43B 43C 43D 43E 43F
44A 44B 44C 44D 44E 44F
45A 45B 45C 45D 45E 45F
46A 46B 46C 46D 46E 46F
47A 47B 47C 47D 47E 47F
48A 48B 48C 48D 48E 48F
49A 49B 49C 49D 49E 49F


- - -
Edit:
I forgot mentioning I borrowed Repeater's version of Seat:: operator<()
Last edited on
wow thanks guys =)
Topic archived. No new replies allowed.