Read access violation

Hello! New to this forum.

My code compiles properly and both requesting and withdrawing functions work properly. When I go to display the landing times/flight numbers, I get the error: std::_String_alloc<std::_String_base_types<char,std::allocator<char> > >::_Mysize(...) returned 0xCDCDCDE1.

No clue what error this is. I've been trying to figure it out but have had no success so far.

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
#include <iostream>
#include <malloc.h>
#include <stdlib.h>
#include <string>

using namespace std;

typedef struct flights
{
	int landingTime;
	string* flightNumber;
	flights* next;
};

int main()
{
	int k;
	cout << "Please enter a K value: ";
	cin >> k;

	flights *head = NULL;
	int choice;
	cout << "-------------------------------------------------" << endl;
	cout << "1. Request Landing" << endl;
	cout << "2. Withdraw Landing Request" << endl;
	cout << "3. List of landing times and the flight number" << endl;
	cout << "4. Quit" << endl;
	cout << "-------------------------------------------------" << endl;
	cout << "What operation would you like to perform? ";
	cin >> choice;

	while (true)
	{
		if (choice == 1)
		{
			if (head == NULL)
			{
				head = (flights*)malloc(sizeof(flights));
				cout << "Please enter the landing time: ";
				cin >> head->landingTime;

				if (head->landingTime >= k)
				{
					cout << "Please enter the flight number: ";
					head->flightNumber = new string;
					cin >> *head->flightNumber;
					head->next = NULL;
					cout << "-------------------------------------------------" << endl;
				}

				else
				{
					cout << "Does not satisfy given K constraint." << endl;
					free(head);
					cout << "-------------------------------------------------" << endl;
				}
			}

			else
			{
				flights *upcomingFlight = head;

				while (upcomingFlight->next != NULL)
				{
					upcomingFlight = upcomingFlight->next;
				}

				flights *newFlight = (flights*)malloc(sizeof(flights));
				cout << "Please enter the landing time: ";
				cin >> newFlight->landingTime;
				bool insertCheck = false;

				if (head->landingTime - newFlight->landingTime >= k && newFlight->landingTime >= k)
				{
					cout << "Please enter the flight number: ";
					head->flightNumber = new string;
					cin >> *head->flightNumber;
					newFlight->next = head;
					head = newFlight;
					insertCheck = true;
					cout << "-------------------------------------------------" << endl;
				}

				else if (newFlight->landingTime - upcomingFlight->landingTime >= k)
				{
					cout << "Please enter the flight number: ";
					head->flightNumber = new string;
					cin >> *head->flightNumber;
					newFlight->next = NULL;
					upcomingFlight->next = newFlight;
					insertCheck = true;
					cout << "-------------------------------------------------" << endl;
				}

				else
				{
					flights *previousFlight = head;
					upcomingFlight = head->next;

					while (upcomingFlight != NULL)
					{
						if (newFlight->landingTime - previousFlight->landingTime >= k && upcomingFlight->landingTime - newFlight->landingTime >= k)
						{
							cout << "Please enter the flight number: ";
							head->flightNumber = new string;
							cin >> *head->flightNumber;
							previousFlight->next = newFlight;
							newFlight->next = upcomingFlight;
							insertCheck = true;
							break;
							cout << "-------------------------------------------------" << endl;
						}

						upcomingFlight = newFlight->next;
						previousFlight = previousFlight->next;

					}

					if (!insertCheck)
					{
						cout << "Does not satisfy given K constraint." << endl;
						free(newFlight);
						cout << "-------------------------------------------------" << endl;
					}
				}
			}
		}

		else if (choice == 2)
		{
			int landingTime;
			flights *prevFlight = head;
			flights *nextFlight = head->next;

			cout << "Please enter the landing time of the flight request you would like to withdraw: ";
			cin >> landingTime;

			if (head->landingTime == landingTime)
			{
				head = head->next;
				free(prevFlight);
				cout << "Flight request has been removed." << endl;
				cout << "-------------------------------------------------" << endl;
			}

			else
			{
				bool withdrawCheck = false;

				while (nextFlight != NULL)
				{
					if (nextFlight->landingTime == landingTime)
					{
						prevFlight->next = nextFlight->next;
						free(nextFlight);
						withdrawCheck = true;
						break;

					}

					prevFlight = prevFlight->next;
					nextFlight = nextFlight->next;
				}

				if (!withdrawCheck)
				{
					cout << "The landing Time you entered was not found." << endl;
					cout << "-------------------------------------------------" << endl;
				}
			}

		}

		else if (choice == 3)
		{
			flights *nextFlight = head;

			if (nextFlight == NULL)
			{
				cout << "There are currently no flights." << endl;
				cout << "-------------------------------------------------" << endl;
			}

			else
			{
				while (nextFlight != NULL)
				{
					cout << "Flight Number: " << *nextFlight->flightNumber << endl;
					cout << "Landing Time: " << nextFlight->landingTime << endl;
					nextFlight = nextFlight->next;
					cout << "-------------------------------------------------" << endl;
				}
			}
		}

		else if (choice == 4)
		{
			break;
		}

		else
		{
			cout << "Not a valid input. Please choose an option from 1-4." << endl;
			cout << "-------------------------------------------------" << endl;
		}

		cout << "What operation would you like to perform? ";
		cin >> choice;
	}

	return 0;
}
Do you have a line number for that error?

Second, you might want to use a switch statement instead of If else if.
Just checked and I get the error on line 102.
provide an example input.

I see malloc, new, free (but no delete), a hand made list, lack of functions.
those are your bullets and you're playing russian roulette
Last edited on
I got a crash at line 188 cout << "Flight Number: " << *nextFlight->flightNumber << endl; when *nextFlight->flightNumber == 0xcdcdcdcd {...} so filghtNumber is invalid

It happend after insering two flight and then choosing "List of landing times and the flight number"
It seems like every time I try to display the landing times and flight numbers, It'll always crash with the same error. Doesn't seem to matter what inputs I put in. The inputs I put in were:

1
5
1
1
9
2
3

and then it crashes. Tested with other numbers and it still gives the same error.
Topic archived. No new replies allowed.