File line missing/overwritten ; reassigning grade

I am having issues with my code overriding some information. For example, when "Joe Sullivan" and his average score is printed out, it prints out twice and totally skips Liz Collins.

I also have no idea to use gradeBook (of type Student) to reassign a grade. Every time I try to reassign the grade, I realize that it is only outputting the assignment # of the last student in the file and trying to go off of that. I have no idea how to fix it to specify which student I need.

Does anyone have any ideas on how to make this work?

Thanks so much!


Here is the file my code will be reading from:
1
2
3
4
5
6
7
Sam Thomas	1	81	90	95	78
Sue Conner	0	67	80	81	79
Dan Gordon	1	90	91	88	93
Pam Roberts	1	93	89	78	90
Joe Sullivan	1	98	91	89	97
Liz Collins	0	57	70	63	72
Kim Shawn	0	89	92	90	92


Here is my 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
 #include <iomanip>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <fstream>
#include <cstring>

using namespace std;

//enum
enum status { UNDERGRADUATE, GRADUATE };

//structures
struct Student
{
	string fullName;
	int* scores;
	status stStatus;
	int average;
	int* max;
};

Student stu;

//function prototypes
void outputA_Struct(Student, int);
int minScore(int*, int);
int* maxScore(int*, int);
void changeScore(Student*, string, int, int, int, int);
int computeAvg(int*, int);
vector<Student> listOfHigherThan_X(Student*, int, int);
vector<Student> listOfAll_As(Student*, int, int);
void outputVector(Student*, vector<Student>, int);

//Bonus prototype
Student** selectSort_ByPtr(Student, int);
void outputSorted(Student*, int);

//my functions
void showMenu();
int underFail(int*, int);
int gradSucc(int*, int);

int main()
{
	//User input for file name
	string fileName;

	//User input for #s
	int numStudents;
	int numScores;

	//Read from file

	string studentL;

	//Read from file
	int score;
	int whatClass;

	//File variable 
	ifstream fileN;

	//Structure Variable


	string end = ".txt";

	cout << "Please enter the name of the file you wish to read from: ";
	getline(cin, fileName);
	fileName.append(end);


	cout << "Please enter the number of students on the file: ";
	cin >> numStudents;

	cout << "Please enter the number of scores per student: ";
	cin >> numScores;

	string studentF;

	int* scoresA = new int[numScores];

	fileN.open(fileName.c_str());

	if (fileN.is_open())
	{
		cout << "yeah ok." << endl;
	}

	else
	{
		cout << "This file could not be opened." << endl;
	}
	int choice;
	int sum = 0;
	int sum2 = 0;
	int key;
	int part;
	int assignmentNum;
	int changeGrad;
	vector<Student> veccy;
	vector<Student> vecc;
	string* names = new string[numStudents];
	Student* gradeBook = new Student[numStudents];
	cout << "Hey bro, can you give me an average grade lol? " << endl;
	cout << "h" << endl;
	cin >> key;
	string name;
	int key2 = 90;
	while (!fileN.eof())
	{
		fileN >> studentF;
		fileN >> studentL;
		studentF.append(" ");
		studentF.append(studentL);
		stu.fullName = studentF;

		for (int i = 0; i < numStudents; i++)
		{
			names[i] = studentF;
		}

		fileN >> whatClass;

		if (whatClass == 0)
		{
			stu.stStatus = UNDERGRADUATE;
		}
		else if (whatClass == 1)
		{
			stu.stStatus = GRADUATE;
		}
		for (int i = 0; i < numScores; i++)
		{
			fileN >> scoresA[i];
		}
		stu.scores = scoresA;
		stu.average = computeAvg(scoresA, numScores);
		if (whatClass == 0)
		{
			int count1 = underFail(scoresA, numScores);
			if (count1 >= 1)
				count1 = 1;
			sum = sum + count1;
		}
		else if (whatClass == 1)
		{
			int count2 = gradSucc(scoresA, numScores);
			if (count2 >= 1)
				count2 = 1;
			sum2 = sum2 + count2;
		}

		for (int i = 0; i < numStudents; i++)
		{
			gradeBook[i].fullName = names[i];
			gradeBook[i].scores = scoresA;
			gradeBook[i].average = computeAvg(scoresA, numScores);
		}
		veccy = listOfHigherThan_X(gradeBook, numStudents, key);
		outputVector(gradeBook, veccy, key);
		vecc = listOfAll_As(gradeBook, numStudents, key2);
		outputVector(gradeBook, vecc, key2);


	}

	cout << "Name? " << endl;
	cin >> name;
	cout << "Which assignment?" << endl;
	cin >> assignmentNum;
	cout << "Value?" << endl;
	cin >> changeGrad;

	changeScore(gradeBook, name, assignmentNum, changeGrad, numScores, numStudents);

	//for option 1
	cout << "Failing undergraduate students: " << sum << endl;

	//for option 2
	cout << "Graduate students with an A: " << sum2 << endl;
	/*
	showMenu();
	cin >> choice;
	fileN.close();
	do {


	} while (choice != 7);
	*/
	int minimum;
	int size = 4;
	minimum = minScore(scoresA, size);
	cout << minimum;
	int* maximum;
	maximum = maxScore(scoresA, size);

	return 0;
}

void showMenu()
{
	cout << "1.\t" << setw(2) << "How many undergraduate students earned at least one failing grade." << endl;
	cout << "2.\t" << setw(2) << "How many graduate students earned at least one grade of A." << endl;
	cout << "3.\t" << setw(2) << "The list of all students with the average score of X or higher." << endl;
	cout << "\t\t" << "What is the target average score?" << endl;
	cout << "4.\t" << setw(2) << "The list of students who made all A's." << endl;
	cout << "5.\t" << setw(2) << "Change a student's grade." << endl;
	cout << "\t\t" << "Name of the student" << endl;
	cout << "\t\t" << "Which assignment, choose in the range of 1 - X" << endl;
	cout << "\t\t" << "Change to what grade?" << endl;
	cout << "6.\t" << setw(2) << "Overall averade grade for graduate and undergraduate students respectively." << endl;
	cout << "7.\t" << setw(2) << "Exit." << endl;
}


//Outputting Joe's information twice, but never outputs Liz's
void outputVector(Student* gradeBook, vector<Student> vecct, int key)
{
	for (int i = 0; i < 1; i++)
	{
		if (gradeBook[i].average > key)
		{
			cout << "Student " << vecct[i].fullName << " with average grade of " << vecct[i].average << endl;
		}
	}

}

int minScore(int* array, int size)
{
	int min = array[0];
	for (int i = 0; i < size; i++)
	{
		if (min > array[i])
		{
			min = array[i];
		}
	}
	return min;
}

int* maxScore(int* arra, int sie)
{
	int count = 0;
	int max = arra[0];
	int* maxi = arra;
	for (int i = 0; i < sie; i++)
	{
		if (max < arra[i])
		{
			max = arra[i];
			maxi++;
		}
	}
	return maxi;
}

int underFail(int* scores, int size)
{
	int count = 0;

	for (int i = 0; i < size; i++)
	{
		if (*(scores + i) < 60)
		{
			count = count + 1;
		}
	}
	return count;
}

int gradSucc(int* scores, int size)
{

	int count = 0;

	for (int i = 0; i < size; i++)
	{
		if (*(scores + i) >= 90)
		{
			count = count + 1;
		}
	}
	return count;
}

int computeAvg(int* scoreA, int size)
{
	int sum = 0;
	int average = 0;

	for (int i = 0; i < size; i++)
	{
		sum = sum + *(scoreA + i);
	}
	average = sum / size;
	return average;
}
vector<Student> listOfAll_As(Student list[], int numStudents, int key)
{
	Student tmp;
	vector<Student> myvec;
	tmp.fullName = stu.fullName;
	tmp.scores = stu.scores;
	tmp.stStatus = stu.stStatus;
	tmp.average = stu.average;
	tmp.max = stu.max;

	for (int i = 0; i < numStudents; i++)
	{
		if (tmp.scores[i] > key)
		{
			myvec.push_back(tmp);
		}
	}
	return myvec;
}

vector<Student> listOfHigherThan_X(Student list[], int numStudents, int key)
{
	Student tmp;
	vector<Student> myvec;
	tmp.fullName = stu.fullName;
	tmp.scores = stu.scores;
	tmp.stStatus = stu.stStatus;
	tmp.average = stu.average;
	tmp.max = stu.max;



	for (int i = 0; i < 1; i++)
	{
		if (list[i].average > key)
		{
			myvec.push_back(tmp);
		}
	}

	return myvec;
}

//I am at a loss on how to take a string name, connect it to a person in gradeBook, use the assignmentNum to find the assignment, and then change the grade.

void changeScore(Student* gradeBook, string name, int assignmentNum, int changeGrad, int numScores, int numStudents)
{
	for (int i = 0; i < numScores; i++)
	{
		if (gradeBook[i].fullName == name)
		{
			gradeBook[i].scores[assignmentNum] = changeGrad;
			
		}
	}
}


Line 156-165: This code runs each time you read a single student. So each time you read a single student, you write that student's info into every entry in the gradebook.
I think you want to append stu to the gradeBook there.


You have a mix of vector's and arrays allocated on the heap. Use vector's throughout.

Line 112 won't work right. eof isn't true until after you try read past the end of the file. Change lines 112-114 to
while (fileN >> studentF >> studentL) {
This will attempt to read the name and then return true of the fileN is still good.

Line 120-123 sent every entry in names[] to the current student's first name. Why do you have the names array at all? You have an array of students and each student has a name. Why duplicate the data somewhere else?

outputVector should just print the contents of vecct. There's no need to pass gradebook or key because you've already set vecct to contain the entries that you want.
Hello!

Thank you for you reply.

I am confused on the issue at lines 156-165. I am not sure how to append gradeBook. I understand the issues with the other lines, though. I've already applied some code.
I think you just want to change gradebook to a vector. Then lines 156-165 become:
gradebook.push_back(stu);

The body of the loop should besically be:
1
2
3
read the info from the file into stu.
Compute the computed bits like aveage
add stu to gradebook.
Topic archived. No new replies allowed.