Why is my code only putting out the last function?

Hi, my code is supposed to read data from a text file and make it into a linked list. There are 6 different choices that are chosen by the corresponding value read in from the file, each calling a function. The program runs, but the only output it does is from the last command in the last function, which is to print the list is empty(After Clearing it). How can I get it to display/do the other functions? A small example of the text file its reading data from:
2 2222
4 2
5
1Pat Green
3478
Junr
3.5
1Sam Jones
3292
Senr
3.2
1Fred Smit
4544
Soph
2.1

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

using namespace std;

struct Student{  //Struct to hold all the data

	string name;
	int id;
	string status;
	double gpa;
	Student* nextStud;


};
Student* createstudent();  //Prototype for createstudent, which reads info from a file and puts the values into declared variables
void addstudent(Student *&FirstStud, Student *NewStud); //Prototype for add student, which adds a new student to the front of the list
void removestudent(Student *FirstStud); //Prototype for removestudent, which removes a target student from the list
void PrintList(Student *FirstStud); //Prototype for PrintList, which displays a list of all students
void ClassList(Student *FirstStud); //Prototype for ClassList, which displays a list of students in a certain class depending on what number between 1 and 4 was entered
void DeansList(Student *FirstStud); //Prototype for DeansList, which displays a list of students with a gpa of 3.5 or higher
void clearlist(Student* FirstStud); //Prototype for clearlist, which removes all students from the list

Student* FirstStud = NULL;  //Initialzes and declares the pointer FirstStud to NULL (0)
Student* NewStud = NULL;    //Initialzes and declares the pointer NewStud to NULL (0)
Student* NextStud=NULL;       //Initialzes and Declares the pointer NextStud to NULL (0)
ifstream infile;        //Declares the variable infile as an ifstream data type
int main(){
	
	infile.open("prog3.txt");  //Opens the file "prog3.txt"
	int choice;       // Declares the variable choice, which will be used to decide which function is called
	infile >> choice;  //Gets the value of choice from the previously opened file
	while (choice >0 && choice <=5){  //While loop that takes the value from the file and assigns it to its correct call, also checks to make sure the choice is between 0-4
		if (choice == 1){        // Checks if the value is 1, which calls createstudent and addstudent if chosen
			NewStud = createstudent();  // Call for createstudent and initialzes the result to NewStud
			addstudent(FirstStud, NewStud);  // Call for addstudent
		}
		else if (choice == 2){  // Checks if the value is 2, which calls removestudent if chosen
			removestudent(FirstStud);  //Call for removestudent
		}
		else if (choice == 3){ // Checks if the value is 3, which calls PrintList if chosen
			PrintList(FirstStud); //Call for PrintList
		}
		else if (choice == 4){  // Checks if the value is 4, which calls ClassList if chosen
			ClassList(FirstStud);  //Call for ClassList
		}
		else if (choice == 5){   // Checks if the value is 5, which calls DeansList if chosen
			DeansList(FirstStud); //Call for removestudent
		} 
		infile >> choice;
	}
		clearlist(FirstStud);  //Call for clearlist

		infile.close();  //Closes the file
		system("pause");
		return 0;


}

Student* createstudent(){  //Function for createstudent

	Student *NewStud = NewStud=NULL;  //Declares the pointer NewStud

	

	
	getline(infile, NewStud->name);  // Reads in name from the text file
	infile >> NewStud->id;	         // Reads in id from the text file
	infile.ignore();
	getline(infile, NewStud->status);    // Reads in status from the text file
	infile >> NewStud->gpa;      // Reads in gpa from the text file
	infile.ignore();
	
	return NewStud;  //Returns the pointer NewStud, and thus all the info stored in its struct

}

void addstudent(Student *&FirstStud, Student *NewStud){  //Function for addstudent
	
	NewStud->nextStud = FirstStud;
	FirstStud=NewStud;  // Sets the NewStud to the front of the list
	cout << FirstStud->name << "was added to the list";  //Reads the name of the student added and displays it
}

void removestudent(Student *FirstStud)  //Function for removestudent
{
	
	int targetid;   //declares targetid, which is the id of the student the program looks for to delete
	Student *NodePtr;    //declares the pointer NodePtr
	Student *PrevNode;    //declares the pointer PrevNode

	if (!FirstStud)  //Makes sure the Node to be removed isnt the first node, deleting the first node would cause memory leak

	{
		cout << "The list is empty" << endl;   //Message that displays the list is empty therefore nothing can be removed
		return;
	
	infile >> targetid;       //Reads an int from the file into targetid
	if (FirstStud->id == targetid)    //Checks to see if the value that was read in is equal to the First stud
	{
		NodePtr = FirstStud;
		FirstStud = FirstStud->nextStud;
		delete NodePtr;
		cout << "Room: " << targetid << " has been removed." << endl;
	}
	else
	{
		NodePtr = FirstStud;

		
		while (NodePtr != NULL && NodePtr->id != targetid)
		{
			PrevNode = NodePtr;
			NodePtr = NodePtr->nextStud;
		}
		if (NodePtr)
		{
			PrevNode->nextStud = NodePtr->nextStud;
			delete NodePtr;
			cout << "Student: " << targetid << " has been removed." << endl;
		}
		else
			cout << "Your value was not on the list" << endl;
	}
	}




	}

//
void PrintList(Student *FirstStud){
	Student* tptr = FirstStud;

	 if (!tptr)

	cout << left << setw(15) << "Name" << setw(15) << "id" << setw(15) << "status"
		<< setw(15) << "gpa" << setw(15) << endl;
	cout << setw(15) << "------" << setw(15) << "----" << setw(15) << "-----"
		<< setw(15) << "-------" << setw(15) << "---------" << endl;
	while (tptr)
	cout << tptr->name << setw(15) << tptr->id << setw(15) << tptr->status << setw(15) << tptr->gpa << endl;
}

void ClassList(Student *FirstStud)
{

	Student *tptr = FirstStud;
	string ClassName;
	int ClassNumber;
	infile >> ClassNumber;

	if (ClassNumber == 1){
		string ClassName = "Fresh";
		cout << "The " << ClassName << " are:" << endl;
	}
	else if (ClassNumber == 2){
		string ClassName = "Soph";
		cout << "The " << ClassName << " are:" << endl;
	}

	else if (ClassNumber == 3){
		string ClassName = "Junr";
		cout << "The " << ClassName << " are:" << endl;
	}
	else if (ClassNumber == 4)
	{
		string ClassName = "Senr";
		cout << "The " << ClassName << " are:" << endl;
	}
	while (tptr){
		if (tptr->status == ClassName)
			cout << tptr->name << tptr->id << tptr->gpa << endl;
		
	}
}

void DeansList(Student *FirstStud){

	 
	if (FirstStud->gpa >= 3.5){
		cout << left << setw(15) << "Name" << setw(15) << "id" << setw(15) << "status"
			<< setw(15) << "gpa" << setw(15) << endl;
		cout << setw(15) << "------" << setw(15) << "----" << setw(15) << "-----"
			<< setw(15) << "-------" << setw(15) << "---------" << endl;
		cout << FirstStud->name << setw(15) << FirstStud->id << setw(15) << FirstStud->status << setw(15) << FirstStud->gpa << endl;
	}
}

void clearlist(Student* FirstStud){

	Student *NodePtr;
	Student *NextStud;

	NodePtr = FirstStud;

	while (NodePtr){

		NextStud = NodePtr->nextStud;
		cout << NodePtr->name << "Has been removed from the list" << endl;
		delete NodePtr;
	}
	FirstStud = NULL;
	cout << "The list is now empty" << endl;
}
The first thing read from the file is 2.
main then calls removestudent(FirstStud); in line 42.
In, void removestudent(Student *FirstStud), if (!FirstStud) of line 95 evaluates to true printing the list is empty and then return ; in line 99 exiting the function and control returns to main.

The next integer from the file 2222 is read at line 52 of main but then the condition while (choice >0 && choice <=5) fails.

control then leaves the while loop to clearlist(FirstStud); in line 54 that prints "The list is now empty" in line 208.

Hope that helps.
How can I get it to display/do the other functions?


I dont understand how the data in your file is structured.
There are 6 different choices that are chosen by the corresponding value read in from the file, each calling a function.

where are the 6 values?
Last edited on
So I want a command that skips the rest of the info for that specific struct and moves onto the beginning of the next struct and reads the next command between 0-5, right?
I also just tried adding a } at the end of here:

1
2
3
4
5
if (!FirstStud)  //Makes sure the Node to be removed isnt the first node, deleting the first node would cause memory leak

	{
		cout << "The list is empty" << endl;   //Message that displays the list is empty therefore nothing can be removed
		return;


Which I think is in the right direction, but doing that gives me a bunch of the same error saying "Cout is ambiguous"
The 6 different choices are 0-5, which in the file are at the beginning of each struct
adding a } after return at line 99 , you must remove one of the } at lines 127,128.
I cant still get the picture of the file. extract any two structures from the file and print here.
Are the floating points part of the choices?
Example of what i'm calling a struct (Taken from the top of the prog3.txt file):

2 2222
4 2
5

Where 2 is the command to select a function to call, 2222 is the name, 4 is the id, 2 is the status (Whether they're a freshman, soph, junr, or senr) and 5 is gpa.

Another example of a "struct"
1Pat Green
3478
Junr
3.5

and im not sure what you mean by "Floating points" Sorry
Last edited on
I now understand that this is a structure
1
2
3
4
1Pat Green
3478
Junr
3.5
as represented by
1
2
3
4
5
6
7
8
struct Student{  //Struct to hold all the data

	string name;
	int id;
	string status;
	double gpa;
	Student* nextStud;
};


The question is.. you said
There are 6 different choices that are chosen by the corresponding value read in from the file, each calling a function
and in one of your latest post,
Where 2 is the command to select a function to call,


so 2 is one of the 6 different choices. What i cant see in the file is the 6 choices that call the 6 functions.
Last edited on
Not quite sure what youre saying/asking where you say "What I cant see in the file is the 6 choices that call the 6 functions" So i'll just link the entire txt file below and explain that
1
2
3
4
5
if (choice == 1){        // Checks if the value is 1, which calls createstudent and addstudent if chosen
			NewStud = createstudent();  // Call for createstudent and initialzes the result to NewStud
			addstudent(FirstStud, NewStud);  // Call for addstudent

		}
This is what the command "1" does

1
2
3
		else if (choice == 2){  // Checks if the value is 2, which calls removestudent if chosen
			removestudent(FirstStud);  //Call for removestudent
		}

This is what the command "2" does

1
2
3
		else if (choice == 3){ // Checks if the value is 3, which calls PrintList if chosen
			PrintList(FirstStud); //Call for PrintList
		}

This is what 3 does

1
2
3
		else if (choice == 4){  // Checks if the value is 4, which calls ClassList if chosen
			ClassList(FirstStud);  //Call for ClassList
		}

This is what 4 does

1
2
3
		else if (choice == 5){   // Checks if the value is 5, which calls DeansList if chosen
			DeansList(FirstStud); //Call for removestudent
		} 

This is what 5 does

And if 0 or a number greater then 5 is chosen it does the clear list function, the 6th function

And here is the whole text file:

2 2222
4 2
5
1Pat Green
3478
Junr
3.5
1Sam Jones
3292
Senr
3.2
1Fred Smit
4544
Soph
2.1
1Jenny Dee
7515
Fresh
2.6
1Frank Watt
8491
Senr
3.6
1Sue Mills
1473
Junr
4.0
1Betty Blue
9448
Soph
3.9
1Henry Pole
6492
Senr
2.5
1Terry Wile
2377
Junr
3.1
1Kenny Sute
4540
Soph
3.7
1Mony Block
8718
Fresh
3.8
1Sara Kire
3453
Senr
1.9
1Jelly Bean
3176
Junr
2.6
1Den Marc
9312
Fresh
2.8
1Sue Danny
8249
Soph
3.7
1Sara Yeavo
2793
Senr
4.0
1Ben Hurr
4816
Fresh
3.5
1Chad Bush
2315
Fresh
2.2
1Kerry Nun
4272
Junr
3.9
1Tom Katty
1275
Junr
3.1
1Vin Onono
1195
Senr
2.4
3
5
4 2
4 3
2 1111
2 1195
3
1Rob Black
3845
Fresh
3.9
1John Fregh
9123
Senr
3.8
2 3478
2 9448
3
4 1
4 4
5
0

Also i'm sorry if what i'm saying isnt making sense or I just made a big circle here, I've been working a lot on this program lately and its frying my brain
Last edited on
sorry PATBALM cannot help you. Still cant understand your text file.
-Data read from the file is sequential and cannot skip from one to another.
Lets dwell on this first part of the text.
1
2
3
4
5
6
7
2 2222
4 2
5
1Pat Green
3478
Junr
3.5

1
2
3
4
5
6
7
8
9
10
-choice read is 2 == > removestudent(FirstStud); called. 
-assume that Firststud != NULL, so infile >> targetid; of line 110 is called.
-Back in main, choice read = 4, ClassList(FirstStud);  called.
-infile >> ClassNumber; then called.
-back in main, choice = 5, DeansList(FirstStud); called.

//this is where it now gets the issue
back in main, 
-choice has to read in an integer but reads 1Pat Green
-every thing goes wrong from here.


May be 1Pat Green is actually supposed to be 1 Pat Green. . note the space btw 1 and Pat.
All right, i'll try spacing out those and see if that fixes it
Topic archived. No new replies allowed.