List all linked list

hello,

I am in the middle of finishing up this code and when i run through the list all it crashes and visual basic throws a exception, but what is happening is that it is listing it all then it will go to nullptr and then crash because there is nothing there. How do i go about fixing this? I have been at this for a while now
lab5class.cpp
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "lab5header.h" 
#include <iostream>
#include <fstream>
#include <iomanip>
/************************************************/
//
//
//
//Lab4(No leaks)
//
//
//
//
/***********************************************/
//initialize pointer
starFleet::starFleet()
{
	//for (int i = 0; i < FLEET_SIZE; i++)
	//{
	//	Ships[i] = NULL; // RM, changed from ships to Ships, as per your class.
	//}
	shipCount = 0;
	head = nullptr;
}
starFleet::~starFleet()
{
    shipData * temp;
    // RM, delete the linked list.
	while(head != nullptr)
	{
		delete[] head->name;
		delete[] head->type;
		delete[] head->position;
		delete[] head->condition;
		delete[] head->captain;
        delete head;
		temp = head->next;
        head = temp;
	}
}
void starFleet::loadData()
{
	shipData * tempShip, *cur, *next;
	shipCount = 0; //set the count = 0
	ifstream inputFile;
	cout << "Loading Data file......." << endl;
	inputFile.open("starships.txt");
	char tempString[S_SIZE];  // RM, temporary holder for input info.
	while (!inputFile.eof())
	{
		tempShip = new shipData;  // RM, Set aside data for a new ship.
		inputFile.getline(tempString, S_SIZE, ';');
		tempShip->name = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
		strcpy(tempShip->name, tempString);  // RM, Copy the name info into the name array.
		inputFile >> tempShip->regNum;
		inputFile.ignore();
		inputFile.getline(tempString, S_SIZE, ';');
		tempShip->type = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
		strcpy(tempShip->type, tempString);
		inputFile.getline(tempString, S_SIZE, ';');
		tempShip->position = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
		strcpy(tempShip->position, tempString);
		inputFile.getline(tempString, S_SIZE, ';');
		tempShip->condition = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
		strcpy(tempShip->condition, tempString);
		inputFile.getline(tempString, S_SIZE, '\n');
		tempShip->captain = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
		strcpy(tempShip->captain, tempString);
		shipCount++;
        
        //  here is where you add a new ship to the linked list, just like addstarship.
        // I'll get you started with the head pointer.
		if (head == nullptr)
		{
			head = tempShip;
		}
		else
		{
			if (head->name > tempShip->name)
			{
				next = head;
				tempShip->next = next;
				
			}
			else
			{
				cur = tempShip;
				next = head->next;
				while (next != nullptr)
				{
					if ((cur->name <= tempShip->name))
					{
						cur->next = head;
						tempShip->next = next;
						break;
					}
					else 
					{
						cur = next;
						next = next->next;
					}
				}
				if (next == nullptr)
				{
					cur->next = tempShip;
				}
			}
			tempShip->next = head;
			head = tempShip;
		}
		
	}
    // RM, moved these lines down.
    inputFile.close();
    cout << endl << "Successfully loaded " << shipCount << " Ships." << endl << endl; // return the total count
    return;
}


void starFleet::addstarship()
{
	//Linked List Data.
	shipData *tempShip, *cur, *next;
	tempShip = new shipData;
	tempShip->next = nullptr;

	fstream file("starships.txt", fstream::app);

	char tempName[S_SIZE];
	cout << " What is the name of the ship ? : ";
	cin.get(tempName, S_SIZE);
	tempShip->name = new char[strlen(tempName) + 1]; // + 1 for null.
	strcpy(tempShip->name, tempName);

	cout << " What is the registry number of the ship ? : ";
	cin >> tempShip->regNum;
	cin.ignore();

	cout << " What is the type of the ship ? : ";
	cin.get(tempName, S_SIZE);
	tempShip->type = new char[strlen(tempName) + 1]; // + 1 for null.
	strcpy(tempShip->type, tempName);
	cin.ignore();

	cout << " What is the position of the ship ? : ";
	cin.get(tempName, S_SIZE);
	tempShip->position = new char[strlen(tempName) + 1]; // + 1 for null.
	strcpy(tempShip->position, tempName);
	cin.ignore();

	cout << " What is the condition of the ship ? : ";
	cin.get(tempName, S_SIZE);
	tempShip->condition = new char[strlen(tempName) + 1]; // + 1 for null.
	strcpy(tempShip->condition, tempName);
	cin.ignore();

	cout << " What is the captain of the ship ? : ";
	cin.get(tempName, S_SIZE);
	tempShip->captain = new char[strlen(tempName) + 1]; // + 1 for null.
	strcpy(tempShip->captain, tempName);
	cin.ignore();
	//  can't use dots with pointers. You have to dereference with (->).
	
	/*************************************************************************/
	//Linked List part of the program. (Lab5)
	//
	//
	//
	//
	/**************************************************************************/
	if (head == nullptr)
	{
		head = tempShip;
	}
	else
	{
		if (head->name > tempShip->name)
		{
			next = head;
			tempShip->next = next;

		}
		else
		{
			cur = tempShip;
			next = head->next;
			while (next != nullptr)
			{
				if ((cur->name <= tempShip->name))
				{
					cur->next = head;
					tempShip->next = next;
					break;
				}
				else
				{
					cur = next;
					next = next->next;
				}
			}
			if (next == nullptr)
			{
				cur->next = tempShip;
			}
		}
		tempShip->next = head;
		head = tempShip;
	}

	file << '\n' << tempShip->name << ';' << tempShip->regNum << ';' <<
		tempShip->type << ';' << tempShip->position << ';' <<
		tempShip->condition << ';' << tempShip->captain;
	shipCount++;
	return;
}

void starFleet::displayFleet()
{
    shipData * cur = head;
	{
		cout << "List all of the fleet!" << endl;
		while(cur != nullptr)
		{
			cout << "-----------------------------------------" << endl;
			cout << "Name of the ship: " << cur->name << endl;
			cout << "Registry number: " << cur->regNum << endl;
			cout << "Type of ship" << cur->type << endl;
			cout << "Position of ship: " << cur->position << endl;
			cout << "Condition of the ship: " << cur->condition << endl;
			cout << "Captain of the ship: " << cur->captain << endl;
			cout << "-----------------------------------------" << endl;
			cur = cur->next;
		}
	}
}
bool starFleet::searchFleet()
{
	int regNum1;
	cout << "Please enter the registry number of the fleet you would like to search for: ";
	cin >> regNum1;
	cin.ignore();
	shipData * cur = head;
	while (cur != nullptr)
	{
		if (cur->regNum == regNum1)
		{
			cout << "Name of the ship: " << cur->name << endl;
			cout << "Registry number: " << cur->regNum << endl;
			cout << "Type of ship" << cur->type << endl;
			cout << "Position of ship: " << cur->position << endl;
			cout << "Condition of the ship: " << cur->condition << endl;
			cout << "Captain of the ship: " << cur->captain << endl;
			cur = cur->next;
			return true;
		}
		else
		{
			cur = cur->next;
		}
		
	}
	if (cur->regNum != regNum1)
	{

		cout << "this ship does not exsist" << endl;
		return false;
	}
}



void starFleet::DeleteShip()
{
	shipData * currentShip, *previousShip, *nextShip;
	char shipName[S_SIZE];
	
	cout << "Delete a Ship." << endl;
	cout << "What is the ship that you would like to Delete?: ";
	cin >> shipName;
	
	bool success = false;
	if (head != nullptr) // Make sure there are numbers in the list.
	{
		if (head->name == shipName)
		{
			currentShip = head;
			head = head->next;
			delete currentShip;
		}
		else
		{
			previousShip = currentShip = head;
			nextShip = head->next;
			while (currentShip != nullptr)
			{
				if (currentShip->name == shipName)
				{
					previousShip->next = nextShip;
					delete currentShip;
					success = true;
					break;
				}
				else
				{
					previousShip = currentShip;
					currentShip = currentShip->next;
					if (currentShip != nullptr)
						nextShip = currentShip->next;
				}
			}
			if (!success)
			{
				cout << "The ship was not found in the list." << endl;
			}
		}
	}
	else
	{
		cout << "The list is empty. :(" << endl;
	}
}


// RM, getCount implementation.
int starFleet::getCount()
{
	return shipCount;
}

lab5header.h
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
#include <iostream>
#include <algorithm>
#ifndef LAB4CLASS_H
#define LAB4CLASS_H
#include <cstring>


/*
THINGS THAT NEED TO BE DONE!!!!!!!

*Sort alphabetical order addShip, loadData.-----DONE

*Fix search

*Fix List all


*/
const int S_SIZE = 256;
const int FLEET_SIZE = 30;
using namespace std;

struct shipData
{
	char* name;
	int regNum;
	char* type;
	char* position;
	char* condition;
	char* captain;
	shipData * next; // can point to next ship
};

class starFleet
{
public:
	void addstarship(); // RM, changed name from addstarships to addstarship.
	void displayFleet();
	void loadData();    // RM, changed to void from int.
	void DeleteShip();
	bool searchFleet();
	int getCount();
	starFleet(); // Constructor
	~starFleet(); //deconstructor
	// Other methods go here.

private:
	int index;
	shipData * head; //Head of linked list
	int shipCount;
};
#endif 
lab5main.cpp
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include "lab5header.h" 

using namespace std;
int main()
{
	starFleet myFleet;
	ofstream outputFile;
	ifstream inputFile;
	int count = 0;
	char choice;
	myFleet.loadData();
	inputFile.close();

	fstream file("starships.txt", fstream::app);
	do {
		cout << "\n**********************************" << endl;
		cout << "Welcome to ****'s star fleet manager" << endl;
		cout << "Here are your following options." << endl;
		cout << "a. Add a fleet" << endl;
		cout << "s. search for a fleet" << endl;				 /*Main Menu*/
		cout << "l. list all fleets" << endl;
		cout << "d. Delete a ship" << endl;
		cout << "q. To quit the fleet manager." << endl;
		cout << "Please use all lower case during the program" << endl;
		cout << "**********************************" << endl;
		cout << "please enter your choice: ";
		cin >> choice;
		cin.ignore(); // Added ignore, to get rid of '\n' stuck in the buffer.

		switch (choice) {
		case 'a':
		case 'A':
			if (myFleet.getCount() < FLEET_SIZE)
			{

				myFleet.addstarship();
				break;
			}
			else
				cout << "Sorry, no more room left in the system.";
			break;
		case 'd':
		case 'D':
		{
					myFleet.DeleteShip();
					break;
		}
		case 's':
		case 'S':
			if (myFleet.getCount() > 0)
			{
				myFleet.searchFleet();
				//class call bool search fleet
				break;
			}
			else
				cout << "sorry no Systems have been added in the database yet" << endl;
			break;
		case 'l':
		case 'L':
			myFleet.displayFleet();
			break;
		case 'q':
		case 'Q':
			cout << "Thank you for using ****'s Star Fleet manager." << endl;
			cout << "Endoy the rest of your day." << endl;
			file.close();
			myFleet.~starFleet();
			system("pause");
			return 0;
			break;
		}
	} while (choice != 'q');

	return 0;
}
Data file
1
2
3
4
5
6
Voyager;74656;Intrepid;Delta_Quadrant;Unknown;Janeway
moon_rocks;420;stoner;pluto;stoned;cap420
Grissom;638;Oberth;Genisis_Planet;Destroyed;Esteban
Enterprise;1701;Constitution;Klingon_Frontier;Operational;Kirk
beta;20;test;test;test;test
alpha;320;test;test;test;test
Mkay.

Run this through your debugger. It will catch the exception on the line where the exception was thrown.

Tell us what line that error occurred on.
cout << "Name of the ship: " << cur->name << endl;

lab5class.cpp 225

runs through all of the ships and then once it runs out of ships to cout then it throws.

Unhandled exception at 0x00E69C82 in LAB5.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD
Last edited on
Sorry, this is gonna take some time.

I decided to slap this into my compiler.

I'm debugging currently. I can tell you that it happens because you're accessing memory you shouldn't be.

You should check for "inputFile.fail()" in your program. If it fails to open, your code will infinitely allocate memory until all the memory in RAM has been used up. It did that to me when I put the text file in the wrong place.
you're right i should do that. Ill add that in right now.
Found it.
The issue is in loadData. When do you assign the last node's next pointer to be nullptr? Or rather, do you ever do this?
I see what you mean now. well looks like I dont ever assign nullptr anywhere. Let me see here hopefully I can figure this out.
You assign everything to your temporary pointer, except 'next'. You can resolve it there.
I got it haha. Just really drained from finals week and this is the last thing i need to turn in i was just stumped. thank you again for all the help.

now to fix my deconstructor and I should be finished..

edit: well just fixed that should be good to go now.
Last edited on
Topic archived. No new replies allowed.