Need help with linked list code for airline reservation

Need help writing a program for an airline reservation system that inputs and outputs the first name, last name, flight number, and boarding priority (Platinum, Gold, Silver, or Lead). From my lecture notes, this is what I've written so far. I ran it by my professor and he said the structure was correct but after trying to run it, I noticed a few errors. The programs compiles and runs, but as soon as I enter a name, the code goes haywire on me. I'm sure the error is happening in the void Airlines::appendNode() portion of the code, but am unsure what is incorrect. Please help
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
#pragma once
#include <iostream>
using namespace std;

const int NAME = 50;

class Airlines
{
	struct ListNode
	{
		char	FName = NAME;
		char	LName = NAME;
		char 	Priority;
		int		FltNum;
		char	value;

		ListNode* next;
	};
		ListNode* head;
	public:
		Airlines() { head = NULL; }
		ListNode pass;
		void appendNode();
		void displayReservation();
};

#include "Airline.h"

void Airlines::appendNode()
{
	ListNode* newNode, *nodePtr;
	newNode = new ListNode;
	cout << "Enter a first name\n";
	cin >> pass.FName;
	cout << "First Name: " << pass.FName << endl;
	newNode->value = pass.FName;
	newNode->next = NULL;

	newNode = new ListNode;
	cout << "Enter a last name\n";
	cin >> pass.LName;
	cout << "Last Name: " << pass.LName << endl;
	newNode->value = pass.LName;
	newNode->next = NULL;

	newNode = new ListNode;
	cout << "Enter a number\n";
	cin >> pass.FltNum;
	cout << "Fight Number: " << pass.FltNum << endl;
	newNode->value = pass.FltNum;
	newNode->next = NULL;

	newNode = new ListNode;
	cout << "Enter a boarding Priority\n";
	cin >> pass.Priority;
	if ((pass.Priority == 'G') || (pass.Priority == 'g'))
		cout << "Your boarding priority is gold\n";
	else ((pass.Priority == 'S') || (pass.Priority == 's'))
		cout << "Your boarding priority is gold\n";
	newNode->value = pass.Priority;
	newNode->next = NULL;

	if (!head)
		head = newNode;
	else
	{
		nodePtr = head;
		while (nodePtr->next)
			nodePtr = nodePtr->next;
		nodePtr->next = newNode;
	}
}


void Airlines::displayReservation(void)
{
	ListNode* nodePtr;
	nodePtr = head;
	if (head == NULL)
		cout << "Your list is empty\n";
	else
	{
		while (nodePtr)
		{
			cout << nodePtr->value << endl;
			nodePtr = nodePtr->next;
		}
	}
}


int main()
{
	Airlines list;
	char prompt;
	char loop = 'y';
	do
	{
		cout << " AIRLINE RESERVATION SYSTEM \n";
		cout << "Press (E) to Enter passenger flight information " << endl;
		cout << "Press (D) to Display the passenger flight information " << endl;
		cout << "Press (Q) to Quit the program " << endl;
		cin >> prompt;

		switch (prompt)
		{
		case 'E':
		case 'e':
			list.appendNode();
			break;

		case 'D':
		case 'd':
			list.displayReservation();
			cout << endl;
			break;

		case 'Q':
		case 'q':
			loop = 'n';
			cout << "\nExiting the program..\n";
			break;

		default: cout << "this is an invalid choice. Please select prompt from the menu.\n";
		}system("pause");
	} while ((loop == 'Y') || (loop == 'y'));

	return 0;
}
Last edited on
What is problem in your current code......?
The problem is is that I'm not exactly sure how to implement the multiple information into a single node or multiple nodes if need be.
Not sure what you are asking. What multiple information do you want in a node? Do you want a linked list of linked lists, for example to put a family together?? Or a fatter node structure, with more info??
Last edited on
I'm assuming its supposed to look something like this (I'm getting errors in Visual Studio). The problem I'm having is trying to get the addPassenger() and selectPriority() parts to append to a node and then display. (Ignore the FltNum down in the main function, I had that there to test the code) Where do I go from here?

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

const short SIZE = 10;
const short PSIZE = 10;
const int NAME = 50;

class Airlines
{
	int pass_count;
	struct PassengerInfo
	{
		char	FName[NAME];
		char	LName[NAME];
		char 	Priority[PSIZE];
		int		FltNum;
		void	addPassenger();
		void	selectPriority();
	};

	class List
	{
		struct ListNode
		{
			PassengerInfo value;
			ListNode* next;
		};
		ListNode* head;
	public:
		List() { head = NULL; }
		void appendNode(const &PassengerInfo);
		void insertNode(const &PassengerInfo);
		void displayReservation();
	};
};

#include "Airline.h"
void Airlines::addPassenger()
{
	bool flt_flag;
	long FName_len = 0;
	long LName_len = 0;

	cout << " ENTER THE PASSENGER(S) FLIGHT RESERVATION(S) \n";

	cin.get();
	cout << "Please enter your first name: ";

	cin.getline(p_info[pass_count].FName, NAME);

	FName_len = cin.gcount();

	while (p_info[pass_count].FName[0] == '\0' || FName_len > 15)
	{
		cout << "\nFirst Name cannot be empty and length MUST be less than 16 characters. \n";

		cout << "Please re-enter your First Name: ";
		cin.getline(p_info[pass_count].FName, NAME);
		FName_len = cin.gcount();
	}

	cout << "Please enter your Last Name: ";

	cin.getline(p_info[pass_count].LName, NAME);

	LName_len = cin.gcount();

	while (p_info[pass_count].LName[0] == '\0' || LName_len > 15)
	{
		cout << "\nLast Name cannot be empty and length MUST be less than 16 characters. \n";

		cout << "Please re-enter your Last Name: ";
		cin.getline(p_info[pass_count].LName, NAME);
		LName_len = cin.gcount();
	}

	selectPriority();

	cout << "Pleas enter your Flight Number: ";
	cin >> p_info[pass_count].FltNum;
	flt_flag = cin.fail();

	while (flt_flag == true)
	{
		cin.clear();
		cin.ignore(10000, '\n');
		cout << "Please re-enter your Flight Number: ";
		cin >> p_info[pass_count].FltNum;
		flt_flag = cin.fail();
	}

	pass_count++;
	system("CLS");
}
void Airlines::selectPriority()
{
		char pr;
		cin.clear();
		cout << "Please enter your boarding priority: " << endl;

		cout << "             BOARDING PRIORITY OPTIONS                \n";

		cout << "(P) - Platinum " << endl;
		cout << "(G) - Gold " << endl;
		cout << "(S) - Silver " << endl;
		cout << "(L) - Lead " << endl;
		cout << "CHOICE: ";
		cin >> pr;

		if (pr == 'P' || pr == 'p')
		{
			strcpy_s(p_info[pass_count].Priority, "Platinum ");
			cout << "\nPriority is \"" << p_info[pass_count].Priority << "\" now.\n\n";
		}
		else if (pr == 'G' || pr == 'g')
		{
			strcpy_s(p_info[pass_count].Priority, "Gold ");
			cout << "\nPriority is \"" << p_info[pass_count].Priority << "\" now.\n\n";
		}
		else if (pr == 'S' || pr == 's')
		{
			strcpy_s(p_info[pass_count].Priority, "Silver ");
			cout << "\nPriority is \"" << p_info[pass_count].Priority << "\" now.\n\n";
		}
		else if (pr == 'L' || pr == 'l')
		{
			strcpy_s(p_info[pass_count].Priority, "Lead ");
			cout << "\nPriority is \"" << p_info[pass_count].Priority << "\" now.\n\n";
		}
		else
		{
			cout << "\nThis is an invalid Boarding Priority, Please re-enter your choices:\n";
			system("pause");
			system("CLS");
			selectPriority();
		}
}

void Airlines::appendNode(const &PassengerInfo)
{
	ListNode* newNode, *nodePtr;
	newNode = new ListNode;
	newNode->value = num;
	newNode->next = NULL;

	if (!head)
		head = newNode;
	else
	{
		nodePtr = head;
		while (nodePtr->next)
			nodePtr = nodePtr->next;
		nodePtr->next = newNode;
	}
}

void Airlines::displayReservation(void)
{
	ListNode* nodePtr;
	nodePtr = head;
	if (head == NULL)
		cout << "Your list is empty\n";
	else
	{
		while (nodePtr)
		{
			cout << nodePtr->value << endl;
			nodePtr = nodePtr->next;
		}
	}
}

void Airlines::insertNode(const &PassengerInfo)
{
	ListNode *newNode, *nodePtr, *previousNode;

	newNode = new ListNode;
	newNode->value = PassengerInfo;

	if (!head)
	{
		head = newNode;
		newNode->next = NULL;
	}

	else
	{
		nodePtr = head;

		while ((nodePtr != NULL) && (nodePtr->value < PassengerInfo))
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		previousNode = nodePtr;
		nodePtr = nodePtr->next;
	}
}

int main()
{
	int FltNum;
	Airlines list;
	char prompt;
	char loop = 'y';
	do
	{
		cout << " AIRLINE RESERVATION SYSTEM \n";
		cout << "Press (E) to Enter passenger flight information " << endl;
		cout << "Press (D) to Display the passenger flight information " << endl;
		cout << "Press (Q) to Quit the program " << endl;
		cin >> prompt;

		switch (prompt)
		{
		case 'E':
		case 'e':
			list.appendNode(PassengerInfo);
			break;

		case 'D':
		case 'd':
			list.displayReservation();
			cout << endl;
			break;

		case 'Q':
		case 'q':
			loop = 'n';
			cout << "\nExiting the program..\n";
			break;

		default: cout << "this is an invalid choice. Please select prompt from the menu.\n";
		}system("pause");
	} while ((loop == 'Y') || (loop == 'y'));

	return 0;
}
Last edited on
cin.getline(p_info[pass_count]. where is pinfo? I cant find it.

Regardless, your add passenger looks OK but incomplete. I can't see that it is actually doing the linked list connections work? I could be missing it, its a lot of code, but is it actually connecting the new node into the data storage?

as select priority is called by add passenger, you should debug that one first. But it looks like more of the same... its operating on data, but is that data in the linked list bit?

Append node isn't calling add passenger. Nothing is.
I'm having trouble seeing how you get to a given node in the list, and then modify or look at it or whatever. Its almost as if you have a working linked list, and a working data object, but they are not really connected. Maybe that was your question..?
Last edited on
Ignore the code above. I've updated the code (I ran it by my professor and he said this version was looking perfect but after trying to run it, I noticed a few errors): I'm only getting two errors, the main one being that I need a ";" after cout << "Your boarding priority is gold\n"; even though I already have a semicolon. I'm hoping this will give me the result I'm looking for. If you see anything else that is incorrect please help me correct it.
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
#pragma once
#include <iostream>
using namespace std;

const int NAME = 50;

class Airlines
{
	struct ListNode
	{
		char	FName = NAME;
		char	LName = NAME;
		char 	Priority;
		int		FltNum;
		char	value;

		ListNode* next;
	};
		ListNode* head;
	public:
		Airlines() { head = NULL; }
		ListNode pass;
		void appendNode();
		void displayReservation();
};

#include "Airline.h"

void Airlines::appendNode()
{
	ListNode* newNode, *nodePtr;
	newNode = new ListNode;
	cout << "Enter a first name\n";
	cin >> pass.FName;
	cout << "First Name: " << pass.FName << endl;
	newNode->value = pass.FName;
	newNode->next = NULL;

	newNode = new ListNode;
	cout << "Enter a last name\n";
	cin >> pass.LName;
	cout << "Last Name: " << pass.LName << endl;
	newNode->value = pass.LName;
	newNode->next = NULL;

	newNode = new ListNode;
	cout << "Enter a number\n";
	cin >> pass.FltNum;
	cout << "Fight Number: " << pass.FltNum << endl;
	newNode->value = pass.FltNum;
	newNode->next = NULL;

	newNode = new ListNode;
	cout << "Enter a boarding Priority\n";
	cin >> pass.Priority;
	if ((pass.Priority == 'G') || (pass.Priority == 'g'))
		cout << "Your boarding priority is gold\n";
	else ((pass.Priority == 'S') || (pass.Priority == 's'))
		cout << "Your boarding priority is gold\n";
	newNode->value = pass.Priority;
	newNode->next = NULL;

	if (!head)
		head = newNode;
	else
	{
		nodePtr = head;
		while (nodePtr->next)
			nodePtr = nodePtr->next;
		nodePtr->next = newNode;
	}
}


void Airlines::displayReservation(void)
{
	ListNode* nodePtr;
	nodePtr = head;
	if (head == NULL)
		cout << "Your list is empty\n";
	else
	{
		while (nodePtr)
		{
			cout << nodePtr->value << endl;
			nodePtr = nodePtr->next;
		}
	}
}


int main()
{
	Airlines list;
	char prompt;
	char loop = 'y';
	do
	{
		cout << " AIRLINE RESERVATION SYSTEM \n";
		cout << "Press (E) to Enter passenger flight information " << endl;
		cout << "Press (D) to Display the passenger flight information " << endl;
		cout << "Press (Q) to Quit the program " << endl;
		cin >> prompt;

		switch (prompt)
		{
		case 'E':
		case 'e':
			list.appendNode();
			break;

		case 'D':
		case 'd':
			list.displayReservation();
			cout << endl;
			break;

		case 'Q':
		case 'q':
			loop = 'n';
			cout << "\nExiting the program..\n";
			break;

		default: cout << "this is an invalid choice. Please select prompt from the menu.\n";
		}system("pause");
	} while ((loop == 'Y') || (loop == 'y'));

	return 0;
}
an else cannot have a condition, did you mean else if?
Thanks. Everything works now. This is what the instructor was looking for with the only note they added being //Need a blank line between passenger outputs
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
#pragma once
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::system;

const int NAME = 50;

class Airlines
{
	struct ReservationNode
	{
		char	FName[NAME];
		char	LName[NAME];
		char 	Priority;
		int	FltNum;

		ReservationNode* next;
		ReservationNode()
		{
			strcpy_s(FName, "");
			strcpy_s(LName, "");
			Priority = 0;
			FltNum = 0;
			next = NULL;
		}
	};
	ReservationNode* head;
public:
	Airlines() { head = NULL; }
	void appendNode();
	void displayReservation();
};
#include "Airline.h"


void Airlines::appendNode()
{
	ReservationNode* newNode, *nodePtr;
	newNode = new ReservationNode;
	cout << "Enter your first name\n";
	cin.ignore();
	cin.getline(newNode->FName, NAME);

	cout << "Enter your last name\n";
	cin.getline(newNode->LName, NAME);

	cout << "Enter your flight number\n"; //also would've been useful to find a way to loop this in the event an incorrent value was inputted 
	cin >> newNode->FltNum;            
	do
	{
	cout << "Enter a boarding Priority\n";
	cout << "(P)latinum, (G)old, (S)ilver, or (L)ead\n";
	cin >> newNode->Priority;
	} while ((newNode->Priority != 'p') && (newNode->Priority != 'P') && (newNode->Priority != 'g') && (newNode->Priority != 'G') && (newNode->Priority != 's') && (newNode->Priority != 'S') && (newNode->Priority != 'l') && (newNode->Priority != 'L'));

	if (!head)
		head = newNode;
	else
	{
		nodePtr = head;
		while (nodePtr->next != NULL)
			nodePtr = nodePtr->next;
		nodePtr->next = newNode;
	}
	system("cls");
}


void Airlines::displayReservation()
{
	ReservationNode* nodePtr;
	nodePtr = head;

	if (nodePtr != NULL)
	{
		cout << "------------------------------------------\n";
		cout << "       Passenger Reservation Info        \n";
		cout << "------------------------------------------\n";
		while (nodePtr)
		{
			cout << "First Name: " << nodePtr->FName << endl;
			cout << "Lirst Name: " << nodePtr->LName << endl;
			cout << "Flight Number: " << nodePtr->FltNum << endl;
			if ((nodePtr->Priority == 'P') || (nodePtr->Priority == 'p'))
				cout << "Your boarding priority is Platinum\n";
			else if ((nodePtr->Priority == 'G') || (nodePtr->Priority == 'g'))
				cout << "Your boarding priority is Gold\n";
			else if ((nodePtr->Priority == 'S') || (nodePtr->Priority == 's'))
				cout << "Your boarding priority is Silver\n";
			else
				cout << "Your boarding priority is Lead\n";
			nodePtr = nodePtr->next;
		}
	}
	else
		cout << "Empty List. Please Enter Reservation Information..\n";
}

int main()
{
	Airlines list;
	char prompt;
	char loop = 'y';
	do
	{
		cout << "-------------------------------------------\n";
		cout << "           RESERVATION SYSTEM           \n";
		cout << "-------------------------------------------\n";
		cout << "Press (E) to Enter passenger flight information " << endl;
		cout << "Press (D) to Display the passenger flight information " << endl;
		cout << "Press (Q) to Quit the program " << endl;
		cin >> prompt;

		switch (prompt)
		{
		case 'E':
		case 'e':
			list.appendNode();
			break;

		case 'D':
		case 'd':
			list.displayReservation();
			cout << endl;
			break;

		case 'Q':
		case 'q':
			loop = 'n';
			cout << "\nExiting the program..\n";
			break;

		default: cout << "this is an invalid choice. Please select prompt from the menu.\n";
		}system("pause");
	} while ((loop == 'Y') || (loop == 'y'));

	return 0;
}
Last edited on
Topic archived. No new replies allowed.