Adding a Node to a Linked List

Hello,

I have run my code through VS debugger, and there are no warnings or errors.

My code reads in information from a file and puts it into a linked list.

For some reason, my addOfficial function isn't working correctly when it reaches the "else" statement. It adds a node to the head with the 'if', but after that it doesn't continue to add nodes to the list. When I call my printAll function, it only displays one node, so I know that is where the error is coming from. I've drawn a diagram of how it is moving and I'm at a loss as to what the problem is. I have posted my code below.

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
#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();
		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;
		}
	}
}
After the first call to addOfficial(), head will point to, say, official1. When official1 was created its next points to NULL, so head->next == NULL.
This makes tptr->next be always false
Line 93 should be outside the loop.
Topic archived. No new replies allowed.