Linked Lists Segmentation Fault

Hello,

I am writing a program that reads in data from a text file and puts it into a linked list. Certain variables are read in only if the "officeHeld" number is a particular number, otherwise the variable is set to 0. When I run this through Visual Studio, I get a blank black screen with nothing on it. When I run it through our university's unix, I get "segmentation fault". I am not sure where the error is here and I'm at a loss. Any help our suggestion would be overwhelmingly appreciated. I'm attaching the code as well as some of the text file information!

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

ifstream inputFile;

struct PublicOfficial
{
	string name;
	string birthState;
	int officeHeld;
	int order;
	int presFirstTerm;
	int presLastTerm;
	int viceFirstTerm;
	int viceLastTerm;
	PublicOfficial *next;
};

void LoadList(PublicOfficial *&head);
void addOfficial(PublicOfficial *&head, PublicOfficial *newNode);
void printOfficials(PublicOfficial *head);

int main()
{
	PublicOfficial *head = NULL;
	PublicOfficial *newNode = NULL;
	LoadList(head);
	printOfficials(head);
	system("pause");
	return 0;
}

void LoadList(PublicOfficial *&head)
{
	PublicOfficial *newNode;
	inputFile.open("officials.txt");
	while (!inputFile.eof())
	{
		newNode = new PublicOfficial;
		newNode->next = NULL;
		getline(inputFile, newNode->name);
		getline(inputFile, newNode->birthState);
		inputFile >> newNode->officeHeld;
		if (newNode->officeHeld == 1 || newNode ->officeHeld == 3)
		{
			inputFile >> newNode->order;
			inputFile >> newNode->presFirstTerm;
			inputFile >> newNode->presLastTerm;
		}
		else
		{
			newNode->order = 0;
			newNode->presFirstTerm = 0;
			newNode->presLastTerm = 0;
		}
		if(newNode->officeHeld == 2 || newNode-> officeHeld == 3)
		{
			inputFile >> newNode->viceFirstTerm;
			inputFile >> newNode->viceLastTerm;
		}
		else
		{
			newNode->viceFirstTerm=0;
			newNode->viceLastTerm=0;
		}
		addOfficial(head, newNode);
	}
	inputFile.close();
}

void addOfficial(PublicOfficial *&head, PublicOfficial *newNode)
{
	if (!head)
	{
		head = newNode;
	}
	else
	{
		newNode->next = head;
		head = newNode;
	}
}

void printOfficials(PublicOfficial *head)
{
	PublicOfficial *tptr = head;
	
	if (!tptr)
	{
		cout << "The list is empty";
	}
	else{

		while (tptr)
		{
			cout << setw(7) << tptr->birthState << setw(12) << tptr->officeHeld <<
				setw(12) << tptr->order << setw(12) << tptr->presFirstTerm << setw(12)
				<< tptr->presLastTerm << setw(12) << tptr->viceFirstTerm << setw(12) << tptr->viceLastTerm;

			tptr= tptr->next;
		}
	}
	cout << endl;
}


Here is a sample piece of the text document (it's a long one so I'm only including a sample, but if I can get it to read even one in, then it's working correctly)

Text File:

WALLACE, HENRY
IOWA
2
33
1941
1945
WASHINGTON, GEORGE
VIRGINIA
1
01
1789
1797
WHEELER, WILLIAM
NEW YORK
2
19
1877
1881
WILSON, HENRY
NEW HAMPSHIRE
2
18
1873
1877

Check to make sure your "officials.txt" file is opening and contains correct data.
Last edited on
When officeHeld equals 2 (line 60), you only extract 2 numbers from your file. Given the file segment you posted ( a big help btw), the second time the loop goes around it tries to extract VIRGINIA into officeHeld.

Never seen *&head before, always used **head.
I have fixed the segmentation error. I was adding my nodes to the beginning of the list rather than the end liked I wanted to.

LowestOne, yes, I noticed the error on that and have fixed it to now grabbing 3 variables.

That being said, I am still having problems ADDING the node to the list. When I call my print function, it only prints the first node, meaning those nodes added within the "else" of addOfficial are not being added correctly. I've stepped through the code, but I'm not sure where my error is :( Once again, nothing comes up on Visual Studio as an error or warning. Officials.txt is opening correctly.

Here is my updated 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
125
126
127
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

ifstream inputFile;

struct PublicOfficial
{
	string name;
	string birthState;
	int officeHeld;
	int presOrder;
	int presFirstTerm;
	int presLastTerm;
	int viceOrder;
	int viceFirstTerm;
	int viceLastTerm;
	PublicOfficial *next;
};

void LoadList(PublicOfficial *&head);
void addOfficial(PublicOfficial *&head, PublicOfficial *newNode);
void printOfficials(PublicOfficial *head);

int main()
{
	PublicOfficial *head = NULL;
	PublicOfficial *newNode = NULL;
	LoadList(head);
	printOfficials(head);
	system("pause");
	return 0;
}

void LoadList(PublicOfficial *&head)
{
	PublicOfficial *newNode;
	inputFile.open("officials.txt");
	while (!inputFile.eof())
	{
		newNode = new PublicOfficial;
		newNode->next = NULL;
		getline(inputFile, newNode->name);
		getline(inputFile, newNode->birthState);
		inputFile >> newNode->officeHeld;
		if (newNode->officeHeld == 1 || newNode ->officeHeld == 3)
		{
			inputFile >> newNode->presOrder;
			inputFile >> newNode->presFirstTerm;
			inputFile >> newNode->presLastTerm;
		}
		else
		{
			newNode->presOrder = 0;
			newNode->presFirstTerm = 0;
			newNode->presLastTerm = 0;
		}
		if(newNode->officeHeld == 2 || newNode-> officeHeld == 3)
		{
			inputFile >> newNode->viceOrder;
			inputFile >> newNode->viceFirstTerm;
			inputFile >> newNode->viceLastTerm;
		}
		else
		{
			newNode->viceOrder = 0;
			newNode->viceFirstTerm=0;
			newNode->viceLastTerm=0;
		}
		inputFile.ignore();
		cout << newNode->name;
		cout << newNode->birthState;
		cout << newNode->officeHeld;
		cout << newNode->presOrder;
		cout << newNode->presFirstTerm;
		cout << newNode->presLastTerm;
		cout << newNode->viceOrder;
		cout << newNode->viceFirstTerm;
		cout << newNode->viceLastTerm;
		cout << endl;
		addOfficial(head, newNode);
	}
	inputFile.close();
}

void addOfficial(PublicOfficial *&head, PublicOfficial *newNode)
{
	PublicOfficial *tptr = NULL;

	if (!head)
	{
		head = newNode;
	}
	else
	{
		tptr = head;
		while (tptr->next)
		{
		tptr = tptr->next;
		tptr->next = newNode;
		}
	}
}

void printOfficials(PublicOfficial *head)
{
	PublicOfficial *tptr = head;
	
	if (!tptr)
	{
		cout << "The list is empty";
	}
	else{

		while (tptr)
		{
			cout << setw(7) << tptr->name << setw(12) << tptr->birthState << setw(12) << tptr->officeHeld <<
				setw(12) << tptr->presOrder << setw(12) << tptr->presFirstTerm << setw(12)
				<< tptr->presLastTerm << setw(12) << tptr->viceOrder << setw(12) << tptr->viceFirstTerm << setw(12) << tptr->viceLastTerm;

			tptr = tptr->next;
		}
	}
}
I should also point out I added the "cout" statements in the LoadList function to make sure it was reading the variables correctly. It is.
Switch line 103 with 104?
Topic archived. No new replies allowed.