Exception thrown in linked list

When I compile and run my code, everything works fine if the first input is 'E'. If my first input is 'D', the compiler closes and I get an exception thrown (read access violation. this->head was nullptr. If there is a handler for this exception, the program may be safely continued.) If you look at void Airlines::displayReservation() I have it set up so that clicking 'D' as a first action will tell the user that the node is null, and up until I finished making modification it was working, now it isn't.

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
#pragma once
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::system;

const int NAME = 50;
const int LEN = 3;

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

		ListNode* next;
		ListNode()
		{
			strcpy_s(FName, "");
			strcpy_s(LName, "");
			Priority = 0;
			FltNum = 0;
			next = NULL;
		}
	};
	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 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";
	cin >> newNode->FltNum;

		cout << "Enter a boarding Priority\n";
		cout << "(P)latinum, (G)old, (S)ilver, or (L)ead\n";
		cin>>newNode->Priority;
		
	if (!head)
			head = newNode;
		else
		{
			nodePtr = head;
			while (nodePtr->next != NULL)
				nodePtr = nodePtr->next;
			nodePtr->next = newNode;
		}
}


void Airlines::displayReservation()
{
	ListNode* nodePtr = head->next;
	nodePtr = head;
	if (nodePtr == NULL)
		cout << "Your list is empty\n";
	else
	{
		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;
		}
	}
}

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;
}


also,in the void Airlines::appendNode(), I originally had the code written as such to account for an incorrect letter typed:
1
2
3
4
5
6
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'))

the problem with this was that when I ran it, I would get an endless loop despite entering a 'p' 'g' 's' or 'l'. Is there another way to rewrite this to that will accomplish what I am trying to do?(adding in a system("cls") would also be helpful).
Last edited on
I would get an endless loop despite entering a 'p' 'g' 's' or 'l'.
The problem is that you use || in your expression for the while loop. It is guaranteed that one of the or'd expression is true. So replace || with &&.
Thanks. Everything works now. I also figured out why the exception was being thrown. 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 
	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;
}
Topic archived. No new replies allowed.