i/o array problem

The program I am trying to get to work takes in a string student ID. 2 double test scores (max score of 200) and 5 double home work scores(max of 265) from a text file. It adds the scores up for test and homework separately then determines an average for them both separately. Then determines a final grade based on the tests counting for 60% of the total grade and the homework is the other 40%.

The problem is the scores are not calculating correctly from the first one on. The math works on paper but not in the program. And after each calculation the previous wrong score adds to the next students data. I think I need to add another loop but I am not sure? Any help would be greatly appreciated!!!!!!

input from the file prg5Data.txt

1
123456789 77.2 88.3 22 28 35 45 33 35 40
2
234567890 97.5 90 25 30 38 48 34 35 50
3
345678901 82.4 77.5 22.5 27 35.5 44 35 33 48


The first calculations are wrong, and it continues summing scores. What the correct score should be is in().

STUDENT ID / HW AVG / TEST AVG / FINAL SCORE / GRADE

123456789 / 98.1(89.8) / 80.0(82.8) / 87.2(85.6) / B
234567890 / 196.2 / 159.9 / 174.4 / A
345678901 / 294.3 / 239.9 /261.6 / A

Number of A's: 2
Number of B's: 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
  #include <iostream>					
#include <fstream>						
#include <string>						
#include <iomanip>					
#include <conio.h>						
using namespace std;					

const int CLASS_SIZE = 30;
const int AMOUNT_OF_TESTS = 2;
const int AMOUNT_OF_HOMEWORK = 7;
const int MAX_STUDENT_ID_LENGTH = 9;
const double PERFECT_HOMEWORK_SCORE = 265;
const double PERFECT_TEST_SCORE = 200;

struct StudentType
{
	string studentID;
	double testGrades,
			homeWorkGrades,
			hwAve,
			testAve,
			finalScore;
	char finalGrade;
};

int getInput(StudentType[], int);
void process(StudentType[], int);
void display(const StudentType[], int);

int main()
{
	
	int totStudents;

	StudentType gradeData[CLASS_SIZE];
	
	totStudents = getInput(gradeData, CLASS_SIZE);
	
	process(gradeData, totStudents);
	display(gradeData, totStudents);
	
	_getch();
	return 0;
}

int getInput(StudentType gradeData[], int classSize)
{	
	int totalStudents = 0;
	int totalTests = 0;
	int totalHomeWork = 0;

	ifstream ifile;

	ifile.open("prg5data.txt");		  
	
	if(ifile.fail())					  	
	{
		cout<< "Can't find file prg5data.txt" << endl << endl;
		_getch();
		return 0;                          
	}

	for(totalStudents = 0; totalStudents < classSize; totalStudents++)
	{
		ifile >> gradeData[totalStudents].studentID;
		
		if(gradeData[totalStudents].studentID.length() <= MAX_STUDENT_ID_LENGTH)
		{
			for(totalTests = 0; totalTests < AMOUNT_OF_TESTS; totalTests++)	
				ifile >> gradeData[totalTests].testGrades;
			for(totalHomeWork = 0; totalHomeWork < AMOUNT_OF_HOMEWORK; totalHomeWork++)
				ifile >> gradeData[totalHomeWork].homeWorkGrades;
		}
		else
		{
			cout << "StudentID: " << gradeData[totalStudents].studentID << " can't be greater than 9 integers. ";
			_getch();
			return 0;
		}
		if(!ifile)
			break;
	}
	ifile.close();
	return totalStudents;
}

void process(StudentType gradeData[], int totStudents)
{
	int processIndex = 0;
	int testAvgIndex = 0;
	int homeWorkAvgIndex = 0;
	double testScoreSum = 0;
	double homeWorkSum = 0;



	for (processIndex = 0; processIndex < totStudents; processIndex++)
	{
		for(testAvgIndex = 0; testAvgIndex < AMOUNT_OF_TESTS; testAvgIndex++)
		{
			testScoreSum += gradeData[testAvgIndex].testGrades;
		}
			
		
		for(homeWorkAvgIndex = 0; homeWorkAvgIndex < AMOUNT_OF_HOMEWORK; homeWorkAvgIndex++)
		{
			homeWorkSum += gradeData[homeWorkAvgIndex].homeWorkGrades;
		}
		
	
		gradeData[processIndex].testAve = ((testScoreSum / PERFECT_TEST_SCORE) * 100);
		gradeData[processIndex].hwAve = ((homeWorkSum / PERFECT_HOMEWORK_SCORE) * 100);
		gradeData[processIndex].finalScore = (gradeData[processIndex].testAve * .6) + (gradeData[processIndex].hwAve * .4);

		if (gradeData[processIndex].finalScore > 90)		
			gradeData[processIndex].finalGrade = 'A';
		else if (gradeData[processIndex].finalScore > 80 && gradeData[processIndex].finalScore < 89.9)
			gradeData[processIndex].finalGrade = 'B';
		else if (gradeData[processIndex].finalScore > 70 && gradeData[processIndex].finalScore < 79.9)
			gradeData[processIndex].finalGrade = 'C';
		else if (gradeData[processIndex].finalScore > 60 && gradeData[processIndex].finalScore < 69.9)
			gradeData[processIndex].finalGrade = 'D';
		else
			gradeData[processIndex].finalGrade = 'E';
	}	
}

void display (const StudentType gradeData[], int totStudents)
{
	ofstream outFile;
	outFile.open("prg6out.txt");
	
	int displayIndex = 0;
	int totA = 0;
	int totB = 0;
	int totC = 0;
	int totD = 0;
	int totE = 0;
	
	outFile << "STUDENT ID" << setw(15) << "HW AVE" << setw(15) << "TEST AVE" << setw(15) << "FINAL SCORE" << setw(15) << "GRADE" << endl;
	outFile << endl;
	for(displayIndex = 0; displayIndex < totStudents; displayIndex++)
	{
		outFile << fixed << showpoint << setprecision(1);
		outFile << gradeData[displayIndex].studentID 
				<< setw(15) << gradeData[displayIndex].hwAve
				<< setw(15) << gradeData[displayIndex].testAve
				<< setw(15) << gradeData[displayIndex].finalScore 
				<< setw(15) << gradeData[displayIndex].finalGrade << endl;

		if (gradeData[displayIndex].finalGrade == 'A')
			totA++;
		else if (gradeData[displayIndex].finalGrade == 'B')
			totB++;	
		else if (gradeData[displayIndex].finalGrade == 'C')
			totC++;	
		else if (gradeData[displayIndex].finalGrade == 'D')
			totD++;
		else
			totE++;	
	}
	outFile << endl;
	outFile << "Number of A's: " << setw(10) << totA << endl
			<< "Number of B's: " << setw(10) << totB << endl
			<< "Number of C's: " << setw(10) << totC << endl
			<< "Number of D's: " << setw(10) << totD << endl
			<< "Number of E's: " << setw(10) << totE << endl;
	outFile.close();
}
Topic archived. No new replies allowed.