Airline Reservation System

I am new to C++ and am required to build an airline reservation system (seems to be a common assignment). I have written pseudo-code with the help of a tutor but would like to know if you could correct what I have so far and guide me to my next steps please (;

Here is a link to the actual instructions: https://docs.google.com/file/d/0B7wq1QknPr2MSW9Pb1AxdDZEbm8/edit

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

class Record
{
private:
	struct Flight
	{
		string FlightNo;
		int seats;
	}
	struct Passenger
	{
		string Name;
		string PhoneNumber;
		string FlightNum;
		string Date;
	}
	vector<Flight> FList;
	vector<Passenger> PList;

public:
	void NewFlight(string FN, int seats)
	{
		for(int i =0; i<FList.size(); i++)
		{
			if(FN.strcmp(FList[i].FlightNo))
			{
				cout << "Flight already exists" << endl;
				return;
			}
		}
		
		Flight Addition = new Flight; // create new struct
		Addition.FlightNo = FN;
		Addition.seats = seats;
		FList.push_back(Addition);
		return;
	}

	void add(string Name, string PhoneNumber, string FlightNum, string Date)
		// go through passenger list to check name and make sure reservation does not exist

		// go through flight and when find flight list check if seats are available. If nothing found, print "no flight exists"

		// otherwise, add object to passenger list

using namespace std;

int main()
{
	
}
using namespace std; should be above any uses of things in ::std, so put it just below your #includes.

Lines 11, 18, and 41 are missing semicolons and you are missing the ending }; to the class.

if(FN.strcmp(FList[i].FlightNo)) ... string doesn't have a member function called strcmp. Use the == operator instead (it's overloaded): if(FN == FList[i].FlightNo) ...

The return; is not necessary on line 38.

Change line 34 to Flight Addition;. There is no need for dynamic memory here.

Other than that, it looks ok so far.
Last edited on
Thank you. I have made changes, but am getting an error with the == operand. Also, where do you suggest I start next?

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
#include <iostream>
#include <vector>
using namespace std;

class Record
{
private:
	struct Flight
	{
		string FlightNo;
		int seats;
	};

	struct Passenger
	{
		string Name;
		string PhoneNumber;
		string FlightNum;
		string Date;
	};

	vector<Flight> FList;
	vector<Passenger> PList;

public:
	void NewFlight(string FN, int seats)
	{
		for(int i =0; i<FList.size(); i++)
		{
			if(FN == FList[i].FlightNo))
			{
				cout << "Flight already exists" << endl;
				return;
			}
		}
		
		Flight Addition;
		Addition.FlightNo = FN;
		Addition.seats = seats;
		FList.push_back(Addition);
	};

	void add(string Name, string PhoneNumber, string FlightNum, string Date)
		// go through passenger list to check name and make sure reservation does not exist

		// go through flight and when find flight list check if seats are available. If nothing found, print "no flight exists"

		// otherwise, add object to passenger list
};

int main()
{
	
}
You have an extra ) on line 30 (that's probably where the error is coming from).

You're still missing a semicolon on line 43, and need to return something from main.
Thank you. I have revised the code again. I am now trying to allow the user to input the name of a file that contains flight numbers followed by the number of seats. I know/think this has something to do with reading a file and loading it into the program, but any guidance would be appreciated.

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
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class ReservationRecords
{
private:
	struct Flight
	{
		string FlightNo;
		int seats;
	};

	struct Record
	{
		string Name;
		string PhoneNumber;
		string FlightNum;
		string DateOfTravel;
	};

	vector<Flight> FList;
	vector<Record> PList;

public:

	// add all info into flightlist
	// function {}

	void NewFlight(string FN, int seats)
	{
		for(int i =0; i<FList.size(); i++)
		{
			if(FN.compare(FList[i].FlightNo))
			{
				cout << "Flight already exists" << endl;
				return;
			}
		}
		
		Flight Addition;
		Addition.FlightNo = FN;
		Addition.seats = seats;
		FList.push_back(Addition);
	};

	void add(string Name, string PhoneNumber, string FlightNum, string Date)
	{
		// go through passenger list to check name and make sure reservation does not exist

		// go through flight and when find flight list check if seats are available. If nothing found, print "no flight exists"

		// otherwise, add object to passenger list
	};
};

int main()
{
	// call build flight list
	
}
Last edited on
Check out this part of the tutorial: http://cplusplus.com/doc/tutorial/files/
Thank you. That helped a little, but how would I prompt the user to enter a file name, then have that data read in to the program, then close the file?
There is another section in the tutorial (beginning probably) that covers how to get input if you need it.
Haven't tested it yet, but how about this? See any errors/flaws?

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
#include <iostream>
#include <fstream> // stream class to both read and write from/to files
#include <string>
using namespace std;

int main () {
	string line;
	string filename;
	cout << "Reservations>> ";
	cin >> filename;
	ifstream  myfile (filename); // stream class to read from files
	if (myfile.is_open())
	{
		while ( myfile.good() )
		{
			getline (myfile,line);
			cout << line << endl; // temporary to test if file is read.
		}
		myfile.close();
	}

	else cout << "Unable to open file";

	return 0;
}
Okay. So this worked. Now my problem is that my input file has the following format:
DL1234 50
DL2134 84
DL0000 2

This is the flight number followed by the seat capacity. How do I go about reading each line, and parsing each data entry (i.e., flight number separated by seat capacity)?
Having trouble writing the delete function. Comments explain what I'd like to do. Any help would be appreciated.

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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

class ReservationRecords
{
private:
	struct Flights
	{
		string FlightNumber;
		int TotalSeats;
		int TakenSeats;
	};

	struct Record
	{
		string Name;
		string PhoneNumber;
		string FlightNumber;
		string DateOfTravel;
	};

	vector<Flights> FlightList;
	vector<Record> RecordList;

public:

	// add all info into flightlist
	void GetFlightData() {
		string filename;
		cout << "Reservations>> ";
		cin >> filename;
		ifstream inputfile (filename); // ifstream = stream class to read from files
		if (inputfile.is_open())
			cout << "Flight numbers and seat availability uploaded\n";
		else
			cout << "ERROR: cannot open input file.\n";

		// reading in flight number and seat capacity from "filename"
		string FilenameFlightNumber;
		int FilenameTotalSeats;

		while (!inputfile.eof()) {// While not at end of inputfile
			inputfile >> FilenameFlightNumber;
			inputfile >> FilenameTotalSeats;

			Flights AddToList;
			AddToList.FlightNumber = FilenameFlightNumber;
			AddToList.TotalSeats = FilenameTotalSeats;
			FlightList.push_back(AddToList);
			};
	};

	void NewFlight(string NewFlightNumber, int NewFlightTotalSeats) {
		for(int i =0; i<FlightList.size(); i++)
		{
			if(NewFlightNumber.compare(FlightList[i].FlightNumber)) // compare compares if equal
			{
				cout << "A flight numbered " << NewFlightNumber << " already exists. Flight not created.\n";
				return;
			}
		}
			Flights AddToList;
			AddToList.FlightNumber = NewFlightNumber;
			AddToList.TotalSeats = NewFlightTotalSeats;
			FlightList.push_back(AddToList);
	};

	void add(string AddName, string AddPhoneNumber, string AddFlightNumber, string AddDate) {
		// go through passenger list to check name and make sure reservation does not exist

		// go through flight and when find flight list check if seats are available. If nothing found, print "no flight exists"

		// otherwise, add object to passenger list
		for(int i=0; i<RecordList.size(); i++)
		{
			if(AddName.compare(RecordList[i].Name) && AddFlightNumber.compare(RecordList[i].FlightNumber) && AddDate.compare(RecordList[i].DateOfTravel))
			{
				cout << "This reservation already exists.\n";
				return;
			}
			else if(AddFlightNumber.compare(RecordList[i].FlightNumber))
			{	// we found the flight
				// check if seats are available
				int j;
				for (j = 0; j < FlightList.size(); j++)
					if (RecordList[i].FlightNumber == FlightList[j].FlightNumber)
						if (FlightList[j].TotalSeats == FlightList[j].TakenSeats)
						{
							cout << "No seat available.\n";
							return;
						}
		
				// seats are available and we can the record
				Record AddToList;
				AddToList.Name = AddName;
				AddToList.PhoneNumber = AddPhoneNumber;
				AddToList.FlightNumber = AddFlightNumber;
				AddToList.DateOfTravel = AddDate;
				RecordList.push_back(AddToList);
				FlightList[j].TakenSeats++;
				return;
			}
		}
		// exit the loop
		// we have not found a flight number
		cout << "No flight numbered " << AddFlightNumber << " exists.\n";
	};

	// Delete record and subtract 1 from taken seats of that specific flight number.
	// If record.name does not exist, print "This reservation does not exist."
	void Delete(string DeleteName, string DeleteFlightNumber, string DeleteDate) {
		for(int i=0; i<RecordList.size(); i++)
		{
			if(DeleteName.compare(RecordList[i].Name) && DeleteFlightNumber.compare(RecordList[i].FlightNumber) && DeleteDate.compare(RecordList[i].DateOfTravel))
			{
				// Delete record [i] and subtract 1 from Flights.TakenSeats
				RecordList[i] = NULL;
				FlightList[i].TakenSeats--;
				return;
			}
		}
		cout << "This reservation does not exist\n";
	};
};

int main()
{
	// call build flight list
};
Last edited on
Topic archived. No new replies allowed.