Load List Not Working Correctly PLEASE HELP!

Hey guys I'm currently writing a program that does a file and then loads the list then must print them. Something is going wrong and I cannot figure out what it is. If anyone here can please try to help me out or give me some logical advice I'm all open to it. This is what I have so far....


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
//Struct 
struct PublicOfficial
{
	string name;
	string sob;
	int code;
	int presorder;
	int firstpterm;
	int lastpterm;
	int vpresorder;
	int firstvpterm;
	int lastvpterm;
	PublicOfficial *nextofficial;
};

//Globals
ifstream infile;

//Prototypes
void LoadList ( PublicOfficial *&firstofficial );
void addOfficial ( PublicOfficial *&firstofficial, PublicOfficial *nextofficial );
void printall ( PublicOfficial *firstofficial, PublicOfficial code);
void printoneterm ( PublicOfficial *firstofficial ); 
int menu();

int main()
{
	PublicOfficial *firstofficial = NULL;
	PublicOfficial code;
	int choice;
	
//Calling LoadList
LoadList ( firstofficial );

//Calling Menu
choice = menu();

while ( choice != 7 )
{
	if ( choice == 1 )
		printall ( firstofficial, code );
	else if ( choice == 2 )
		printall ( firstofficial, code );
	else if ( choice == 3 )
		printall ( firstofficial, code );
	else if ( choice == 4 )
		printoneterm ( firstofficial );
	//Calling Menu
	menu();
}
return 0;

}	// End of Main

void LoadList ( PublicOfficial *&firstofficial )
{
	PublicOfficial *newofficial;
	//Opening the file
	infile.open( "officials.txt" );
	if (!infile)
	{
		cout << "This file officials.txt does not exist." << endl;
		exit(1);
	}
	

	while (!EOF)
	{
		
		newofficial = new PublicOfficial;
		newofficial->nextofficial= NULL;
		getline(infile,newofficial->name);
		getline(infile,newofficial->sob);
		infile >> newofficial->code;
			if ( newofficial->code == 3 )
			{
				infile >> newofficial->presorder;
				infile >> newofficial->firstpterm;
				infile >> newofficial->lastpterm;
				infile >> newofficial->vpresorder;
				infile >> newofficial->firstvpterm;
				infile >> newofficial->lastvpterm;
			}
			else if ( newofficial->code == 2 )
			{
				newofficial->presorder = 0;
				newofficial->firstpterm = 0;
				newofficial->lastpterm= 0;
				infile >> newofficial->vpresorder;
				infile >> newofficial->firstvpterm;
				infile >> newofficial->lastvpterm;
			}
			else if (newofficial->code == 1)
			{
				infile >> newofficial->presorder;
				infile >> newofficial->firstpterm;
				infile >> newofficial->lastpterm;
				newofficial->vpresorder = 0;
				newofficial->firstvpterm = 0;
				newofficial->lastvpterm = 0;
			}
		infile.ignore();
		//Calling Add Official
		addOfficial ( firstofficial, newofficial );
	}
	//Closing the file
	infile.close(); 

}
void addOfficial ( PublicOfficial *&firstofficial, PublicOfficial *newofficial )
{
	PublicOfficial *Tptr;
	Tptr = firstofficial;
	firstofficial = newofficial;
	newofficial->nextofficial = Tptr;
}
int menu ()
{
	int choice;
	
	cout << endl;
	cout << "========================================" << endl;
	cout << " 1. List Presidents " << endl;
	cout << " 2. List Vice Presidents " << endl;
	cout << " 3. List Officials that held both " << endl;
	cout << " 4. List One Term Presidents " << endl;
	cout << " 5. List Presidents by the century " << endl;
	cout << " 6. List Presidents by birth place " << endl;
	cout << " 7. Quit " << endl;
	cout << "========================================" << endl;
	cout << "Please enter your selection." << endl;
	cin >> choice;
	//Checking whether choice is vaild
	while ( choice < 1 || choice > 7 )
	{
		cout << "This is an incorrect choice, please choose another: ";
		cin >> choice;
	}
	return choice;
}
void printall ( PublicOfficial *firstofficial, PublicOfficial code)
{
	PublicOfficial *Tptr = firstofficial;
	
	while (Tptr)
	{
		cout << "Name:" << setw(30) << Tptr->name << endl;
		cout << "State:" << setw(30) << Tptr->sob << endl;
		if (Tptr->code == 1)
		{
		cout << "President #:" << setw(30) << Tptr->presorder << endl;
		cout << "Presidential Term:" << setw(30) << Tptr->firstpterm
		<< " - " << Tptr->lastpterm << endl;
		cout << "Vice President #:" << setw(30) << " N/A " << endl;
		cout << "Vice Presidential Term:" << setw(30) << " N/A " << endl;
		}
		else if (Tptr->code == 2 )
		{
		cout << "President #:" << setw(30) << "N/A" << endl;
		cout << "Presidential Term:" << setw(30) << "N/A" << endl;
		cout << "Vice President #:" << setw(30) << Tptr->vpresorder << endl;
		cout << "Vice Presidential Term:" << setw(30) << Tptr->firstvpterm
		<< " - " << Tptr->lastvpterm << endl;
		}
		else if ( Tptr->code == 3 )
		{
		cout << "President #:" << setw(30) << Tptr->presorder << endl;
		cout << "Presidential Term:" << setw(30) << Tptr->firstpterm
		<< " - " << Tptr->lastpterm << endl;
		cout << "Vice President #:" << setw(30) << Tptr->vpresorder << endl;
		cout << "Vice Presidential Term:" << setw(30) << Tptr->firstvpterm
		<< " - " << Tptr->lastvpterm << endl;
		}
		else
			cout << "Error" << endl;
		Tptr = Tptr->nextofficial;
	}
}
Last edited on
(1) you dont have a constructor for your struct PublicOfficial.
(2) you dont have a function called printoneterm
(3) you never instantiated firstofficial, it currently is a null reference
(4) last but not least im a little confused by your use of *& next to each other. I would drop the & from all pointers.

Just make sure you '#include' all necessary classes.
after that your program ran fine. (well it never loaded the officials.txt file cause i dont have it).

Let me know if you need anything else.
Another thing to note. Write you code in pieces, then test those pieces before you move on. It saves time on debugging.

Example:

I would have written the struct first, then tested to see if I could instantiate it and all the member variables were right. Then write the loadList() function, test that, and on and on.

also, try to use camel case. Better programming practice.
Topic archived. No new replies allowed.