Why is my program crashing? Reference error?

Hello, so I've written a program that links trains structures together through pointers and I have no errors but it crashes every time I try to run it it crashes right from the start and does not make it past the cout of test 1. Why is is it crashing? I think it might be because of a reference that I didn't allocate properly but I'm not very sure. Hence my question, why is my program crashing and how can I fix it so it works?

Thanks!

PS: I'm only using char strings because my class has to, not because they are my preferred data type.

Code:

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
#include <iostream>
#include <cstring>
#include "Train.h"

using namespace std;

struct TrainCar {

char * type[50];
float capacity;
int serial;
TrainCar *nextTrain = NULL;

};

TrainCar * createTrainCar(char *kind, int cargo, int serial) {

TrainCar *newTrain;
newTrain = new TrainCar;

strcpy(*newTrain->type, kind);
newTrain->capacity = cargo;
newTrain->serial = serial;

return newTrain;

}


void connectTrains(TrainCar *front, TrainCar *back) {

TrainCar *trainWalker;
TrainCar *tempCar;

trainWalker = front;

if (front == NULL) {

return;
}

while (trainWalker != NULL) {

	tempCar = trainWalker;
	trainWalker = trainWalker->nextTrain;
}

tempCar->nextTrain = back;

}


TrainCar * disconnectTrains(TrainCar train, int serial) {

	TrainCar *trainWalker = NULL, *tempCar;
	*trainWalker = train;


	// walk along until the end, or unti you find the car
	while ((trainWalker != NULL) && (trainWalker->serial != serial)) {

		trainWalker = trainWalker->nextTrain;
	}

	if (trainWalker == NULL) {

		return NULL;
		}

	tempCar = trainWalker->nextTrain;
	trainWalker->nextTrain = NULL;

return tempCar;
}


void printTrain(TrainCar *train) {

	TrainCar *trainToPrint = train;

		while (trainToPrint != NULL) {

			cout << "Type: " << trainToPrint->type << endl;
			cout << "Capacity: " << trainToPrint->capacity << endl;
			cout << "Serial Number: " << trainToPrint->serial << endl;
			cout << "\n" << endl;

			trainToPrint = trainToPrint->nextTrain;

		}

}


int main() {

	cout << "Test 1" << endl;
	TrainCar *a = createTrainCar("engine", 0, 111);
	TrainCar *b = createTrainCar("tanker", 8000, 115);
	TrainCar *c = createTrainCar("tanker", 8000, 117);
	TrainCar *d = createTrainCar("cargo", 6000, 214);
	TrainCar *e = createTrainCar("cargo", 5000, 260);
	TrainCar *f = createTrainCar("stock", 5000, 270);

	cout << "Test 2" << endl;

	connectTrains(a, b);
	connectTrains(a, c);
	connectTrains(a,d);
	connectTrains(a,e);
	connectTrains(a,f);

	cout << "Test 3" << endl;


	printTrain(a); // prints them all



	cout << "Test 4" << endl;

	return 0;
}
Last edited on
My first question is what is with all the pointers?

@jlb I would prefer not to use them if I don't have to but my assignment requirements state that we need to use pointers for this assignment. The "next" one in the struct should point to the next train to link all of the structs (sort of like a list), as for the other ones, I'm not sure, I'm very new to pointers so I don't always use/implement them correctly.
1
2
struct TrainCar {
  char * type[50];
.¿what do you think `type' is?


> it crashes every time I try to run it it crashes right from the start and
> does not make it past the cout of test 1.
debugger, backtrace.
Last edited on
@ne555 ah I see, I took away the pointer on the struct and changed a couple other things and it works now, thanks!
Last edited on
Topic archived. No new replies allowed.