exception thrown visual studio

I'm working on my lab for a class and my code is throwing an exception and I'm not sure how to fix it. I would really appreciate some help on what to do because I'm at a complete loss. The last thing I did was change my strtok to strtok_s in my load function but I'm not sure if that's the cause or not.


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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

const int MAX_CHAR = 101;

class Wagon
{
public:
	Wagon();
	Wagon(const char Name[], const char Team[], float Weight, float Animals);
	Wagon(const Wagon& wagon);
	~Wagon();

	void operator= (const Wagon& wagon);
	void nameget(char Name[]) const;
	void teamget(char Team[]) const;
	void nameset(const char Name[]);
	void teamset(const char Team[]);
	void weightset(float Weight);
	void animalsset(float Animals);
	float weightget() const;
	float animalsget() const;
	void wagonget();
	void create(const char Name[], const char Team[], float Weight, float Animals);
private:
	char* Name;
	char* Team;
	float	Weight;
	float	Animals;
};

class Wagonlist
{
public:
	Wagonlist();
	~Wagonlist();

	void addwagon(const Wagon& wagon);
	void removewagon(const Wagon& wagon);
	void print(const Wagon& wagon);
	void loadwagons(const char file[]);
	void savewagons(const char file[]) const;
private:
	struct Node
	{
		Node(const Wagon& wagon)
		{
			list = wagon;
			next = nullptr;
			index = 0;
		}

		int index;
		Wagon list;
		Node* next;
	};

	Node* head;
	Node* tail;
	int	size;
};


void displayMenu();
char commandget();
void execute(char command, Wagonlist& list);

int main()
{
	char		command;
	Wagonlist	collection;
	char		file[] = "wagons.txt";

	collection.loadwagons(file);
	displayMenu();
	command = commandget();

	while (command != 'q')
	{
		execute(command, collection);
		displayMenu();
		command = commandget();
	}

	//collection.savesongs(file);
	return 0;
}

Wagonlist::Wagonlist()
{
	head = nullptr;
	tail = nullptr;
	size = 0;
}

Wagonlist::~Wagonlist()
{
	Node* curr = head;

	while (curr)
	{
		head = curr->next;
		delete curr;
		curr = head;
	}
}


void Wagonlist::addwagon(const Wagon& wagon)
{
	Node* newnode = new Node(wagon);
	newnode->next = head;
	head = newnode;
	size++;
}


void Wagonlist::print(const Wagon& wagon)
{
	char	Name[MAX_CHAR];
	char	Team[MAX_CHAR];
	int		Weight;
	int		Animals;
	int		i;

	Node* curr = head;
	i = 0;

	while (curr)
	{
		cout << "[" << i << "]" << endl;
		curr->list.nameget(Name);
		curr->list.teamget(Team);
		Weight = curr->list.weightget();
		Animals = curr->list.animalsget();

		cout << "Family Name: " << Name << endl;
		cout << "Team: " << Team << endl;
		cout << "Weight: " << Weight << endl;
		cout<<"Number of Animals: " << Animals << endl;
		curr = curr->next;
		i++;
	}
}

void Wagonlist::loadwagons(const char file[])
{
	char		buffer[MAX_CHAR];
	char*		pch;
	char        Name[MAX_CHAR];
	char        Team[MAX_CHAR];
	float       Weight;
	float       Animals;
	int         counter;
	int         length;
	char* next_token;
	Wagon		currwagon;
	ifstream    in;
	//rsize_t strmax;
	in.open("wagons.txt");

	if (!in)
	{
		exit(0);
	}

	while (in.getline(buffer, MAX_CHAR))
	{
		char temp[4][MAX_CHAR];
		counter = 0;
		//rsize_t strmax = sizeof buffer;
		pch = strtok_s(buffer, "|", &next_token);

		while (pch != nullptr)
		{
			strcpy_s(temp[counter], pch);
			pch = strtok_s(nullptr, "|", &next_token);
			counter++;
		}

		strcpy_s(Name,MAX_CHAR, temp[0]);
		strcpy_s(Team, MAX_CHAR, temp[2]);
		Weight = atoi(temp[1]);
		Animals = atoi(temp[3]);
		length = strlen(Name);

		currwagon.nameset(Name);
		currwagon.teamset(Team);
		currwagon.weightset(Weight);
		currwagon.animalsset(Animals);

		Node* newnode = new Node(currwagon);
		Node* curr = head;

		if (!curr)
		{
			head = newnode;
			tail = newnode;
		}
		else
		{
			while (curr->next)
			{
				curr = curr->next;
			}

			curr->next = newnode;
		}
		tail = newnode;
		size++;
	}
	in.close();
}

void Wagonlist::savewagons(const char file[]) const
{
	char		Name[MAX_CHAR];
	char		Team[MAX_CHAR];
	float		Weight;
	float		Animals;
	
	ofstream	out;
	Node* curr;

	out.open("wagons.txt");
	if (!out)
	{
		cout << "Something went wrong while opening the file." << endl;
		exit(1);
	}

	for (curr = head;curr;curr = curr->next)
	{
		curr->list.nameget(Name);
		curr->list.teamget(Team);
		Weight = curr->list.weightget();
		Animals = curr->list.animalsget();

		out << Name << '|' << Team << '|' << Weight << '|' << Animals << endl;
	}

	cout << "Your wagons have been documented." << endl;
	out.close();
}

void Wagonlist::removewagon(const Wagon& wagon)
{
	int     count;
	int		i;

	Node* curr = new Node(wagon);
	Node* prev = new Node(wagon);
	curr = head;
	prev = head;
	i = 0;
	count = 0;

	cout << "Please enter the index number of the wagon that you wish to remove." << endl;
	cin >> i;
	cin.ignore();
	cin.clear();

	while (curr)
	{
		prev = curr;
		curr = curr->next;
		if (i == 0)
		{
			head = curr;
			return;
		}
		else if (i == count)
		{
			curr = curr->next;
			prev->next = curr;
			return;
		}
		count++;
	}
	size--;
	delete prev;
	delete curr;
}

void displayMenu()
{
	cout << "Please select what you want to do.";
	cout << endl << "Welcome to Uwagons!" << endl
		<< "     a. Add a wagon " << endl
		<< "     l. Show all wagons " << endl
		<< "     r. Remove a wagon " << endl
		<< "     q. Quit " << endl << endl;
}

char commandget()
{
	char	cmd;

	cin.get(cmd);
	cin.ignore(100, '\n');
	return (cmd);
}

void execute(char cmd, Wagonlist& list)
{
	Wagon	wagon;

	switch (cmd)
	{
	case 'a':
		wagon.wagonget();
		list.addwagon(wagon);
		break;
	case 'l':
		list.print(wagon);
		break;
	case 'r':
		list.removewagon(wagon);
		break;
	case 'q':
		cout << "goodbye" << endl;
	default:
		cout << "You have entered an illegal command. Please try again. " << endl;
		break;
	}
}


Wagon::Wagon()
{
	create("No Family Name", "No Team", 0, 0);
}

Wagon::Wagon(const char Name[], const char Team[], float Weight, float Animals)
{
	create(Name, Team, Weight, Animals);
}

void Wagon::create(const char Name[], const char Team[], float Weight, float Animals)
{
	this->Name = new char[strlen(Name) + 1];
	this->Team = new char[strlen(Team) + 1];
	strcpy_s(this->Name, MAX_CHAR, Name);
	strcpy_s(this->Team, MAX_CHAR, Team);
	this->Weight = Weight;
	this->Animals = Animals;
}

Wagon::Wagon(const Wagon& wagon)
{
	this->Name = nullptr;
	this->Team = nullptr;
	*this = wagon;
}

Wagon::~Wagon()
{
	if (this->Name)
	{
		delete[] this->Name;
	}

	if (this->Team)
	{
		delete[] this->Team;
	}
}

void Wagon::nameget(char Name[]) const
{
	strcpy_s(Name, MAX_CHAR, this->Name);
}

void Wagon::teamget(char Team[]) const
{
	strcpy_s(Team, MAX_CHAR, this->Team);
}

void Wagon::nameset(const char newname[])
{
	if (Name)
	{
		delete[] Name;
	}

	Name = new char[strlen(newname) + 1];
	strcpy_s(Name, MAX_CHAR, newname);
}

void Wagon::teamset(const char newteam[])
{
	if (Team)
	{
		delete[] Team;
	}

	Team = new char[strlen(newteam) + 1];
	strcpy_s(Team, MAX_CHAR, newteam);
}

void Wagon::weightset(float Weight)
{
	this->Weight = Weight;
}

void Wagon::animalsset(float Animals)
{
	this->Animals = Animals;
}

float Wagon::weightget() const
{
	return Weight;
}

float Wagon::animalsget() const
{
	return Animals;
}

void Wagon::wagonget()
{
	char            Name[MAX_CHAR];
	char            Team[MAX_CHAR];
	float           Weight;
	float           Animals;

	cin.clear();
	cout << "Please enter your wagon's family name: ";
	cin.get(Name, MAX_CHAR, '\n');
	cin.ignore();

	cout << "Please enter your wagon's team: ";
	cin.get(Team, MAX_CHAR, '\n');
	cin.ignore();

	cout << "Please enter your wagon's weight: ";
	cin >> Weight;

	while (cin.fail())
	{
		cin.clear();
		cin.ignore(4, '\n');
		cout << "You have entered an invalid character, Please try again: ";
		cin >> Weight;
		cin.ignore(4, '\n');
	}

	cin.clear();
	cout << "Please enter the number of animals on your team: ";
	cin >> Animals;

	while (cin.fail())
	{
		cin.clear();
		cin.ignore(3, '\n');
		cout << "You have entered an invalid character, Please try again: ";
		cin >> Animals;
		cin.ignore(3, '\n');
	}

	cin.clear();
	nameset(Name);
	teamset(Team);
	weightset(Weight);
	animalsset(Animals);
}

void Wagon::operator=(const Wagon& wagon)
{
	if (this == &wagon)
	{
		return;
	}

	nameset(wagon.Name);
	teamset(wagon.Team);
	weightset(wagon.Weight);
	animalsset(wagon.Animals);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
	Boyd|3300|Oxen|2
	Cary|7806|Oxen|4
	Barker|4707|Horse|4
	Boggus|4700|Oxen|2
	Bond|3600|Horse|4
	Applegate|5522|Oxen|4
	Burnett|3700|Horse|4
	Arthur|2219|Oxen|2
	Adams|4505|Oxen|4
	Aiken|6205|Oxen|4
	Beagle|6666|Oxen|4
1
2
char* Name;
char* Team;

Why are you not using std::string for these?

Most of your usage of char pointers inside your Wagon class are broken in one way or another.

1
2
3
4
this->Name = new char[strlen(Name) + 1];
	this->Team = new char[strlen(Team) + 1];
	strcpy_s(this->Name, MAX_CHAR, Name);
	strcpy_s(this->Team, MAX_CHAR, Team);

"safe" functions are only safe when you're honest about buffer sizes.

1
2
3
4
5
6
Wagon::Wagon(const Wagon& wagon)
{
	this->Name = nullptr;
	this->Team = nullptr;
	*this = wagon;
}

You've implemented a 'shallow' copy. That is, you now have two Wagons with identical pointers to the same Name. The problem is, when you delete one of them, the other winds up with a stale pointer.
You need to do the whole 'new char...' thing and make an actual copy of each char pointer.
https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy

1
2
3
4
void Wagon::nameget(char Name[]) const
{
	strcpy_s(Name, MAX_CHAR, this->Name);
}

Pass the size of the buffer as well, and use that size in your strcpy_s

There are some things about your head/tail pointer list usage which seem off as well.
I would suggest you make specific functions just to manage the insertion of a Node into your list, rather than burying it in the flow of some other functionality

Topic archived. No new replies allowed.