Airline Reservation System (argc/argv and parsing data)

Having trouble with argc/argv and parsing the data in my "void GetFlightData" function. Any help 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#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) {
		// 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."
	// delete <name> from <flight-number> on <date>
	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].Name = "";
				RecordList[i].FlightNumber = "";
				RecordList[i].DateOfTravel = "";
				FlightList[i].TakenSeats--;
				return;
			}
		}
		cout << "This reservation does not exist\n";
	};

	// is <name> in <flight-number> on <date>?
	void IsNameInFlightOnDate(string IsNameInFlightOnDateName, string IsNameInFlightOnDateFlightNumber, string IsNameInFlightOnDateDate) {
			for(int i=0; i<RecordList.size(); i++)
		{
			if(IsNameInFlightOnDateName.compare(RecordList[i].Name) && IsNameInFlightOnDateFlightNumber.compare(RecordList[i].FlightNumber) && IsNameInFlightOnDateDate.compare(RecordList[i].DateOfTravel))
			{
				cout << "Yes\n";
				return;
			}
		}
		cout << "No\n";
	};

	// lookup <name>
	void Lookup(string LookupName) {
		int counter = 0;
		for(int i=0; i<RecordList.size(); i++)
		{
			if(LookupName.compare(RecordList[i].Name))
			{
				counter++;
				cout << counter << ". (" << LookupName << ", " << RecordList[i].PhoneNumber << ", " << RecordList[i].FlightNumber << ", " << RecordList[i].DateOfTravel << ")\n";
			}
		}
		if (counter == 0)
			cout << "No reservation exists for " << LookupName << ".\n";
	};

	// display <flight-number> on <date>
	void Display(string DisplayFlightNumber, string DisplayDate) {
			int counter = 0;
			for(int i=0; i<RecordList.size(); i++)
		{
			if(DisplayFlightNumber.compare(RecordList[i].FlightNumber) && DisplayDate.compare(RecordList[i].DateOfTravel))
			{
				counter++;
				cout << counter << ". (" << RecordList[i].Name << ", " << RecordList[i].PhoneNumber << ", " << RecordList[i].FlightNumber << ", " << RecordList[i].DateOfTravel << ")\n";
			}
		}
		if (counter == 0)
			cout << "No flight numbered " << DisplayFlightNumber << " exists.\n";
	};
};

// argc gives total # of command line arguments entered by user, including program name. redtxt output.txt returns 2.
// argv is an array of strings that contains all arguments on the command line, starting with the program name.
// In this ex, argv[0] points to readtxt, and argv[1] points to output.txt
int main(int argc, char *argv[])
{
	ReservationRecords R;
	string filename = argv[1];

	// call build flight list
	R.GetFlightData(filename);

	string str1, str2, str3, str4, str5;

	while(1)
	{
		cin >> str1;
		if (str1.compare("new"))
		{
			int s;
			cin >> str2;
			if (str2.compare("flight"))
			{
				cin >> str3;
				cin	>> s;
			}
		
			R.NewFlight(str3, s);
		}
	
		else if (str1.compare("quit"))
			return 0;

		else if (str1.compare("add"))
		{
			cin >> str2;
			cin >> str3;
			cin >> str4;
			cin >> str5;

			R.add(str2, str3, str4, str5);
		}

		else if (str1.compare("delete"))
		{
			cin >> str2;
			cin >> str3;
			cin >> str3;
			cin >> str4;
			cin >> str4;

			R.Delete(str2, str3, str4);
		}

		else if (str1.compare("is"))
		{
			cin >> str2;
			cin >> str3;
			cin >> str3;
			cin >> str4;
			cin >> str4;

			R.IsNameInFlightOnDate(str2, str3, str4);
		}

		else if (str1.compare("lookup"))
		{
			cin >> str2;
			R.Lookup(str2);
		}
	
		else if (str1.compare("display"))
		{
			cin >> str2;
			cin >> str3;
			cin >> str3;
			R.Display(str2, str3);
		}

		else cout << "\n\nInvalid input\n\n";
	}
};
Topic archived. No new replies allowed.