# calcuate average test grade problem

• 11-15-2008
syrup890
I'm currently taking a C++ class and this is a task we have to complete. I've got most of it completed, but I have one problem.

The way the program is suppose to work is it's suppose to read an infile that has an unknown number of students with their 5 best test grades. It's suppose to calculate the average for each student, then the average for the all the students. It's suppose to then count the numbers of A's, B's C's...etc there are within the students.

Here's what I have...

Code:

```void main () { ... calls all functions and any couts ..} void take_input(int &test1, int &test2, int &test3, int &test4, int &test5, int &students, string &student) {         int numOfStud =0;         ifstream inData;         inData.open("in.txt");         while (!inData.eof())         {                 inData>>student>>test1>>test2>>test3>>test4>>test5;                 cout<<student<<test1<<test2<<test3<<test4<<test5<<endl;                 students++;         } } float calculateAverage(int &test1, int &test2, int &test3, int &test4, int &test5, int &students, string &student, float &average) {         int sum;         float classAve, x = 0;         for (int i = 1; i <= students; i++)         {                 sum = test1 + test2 + test3 + test4 + test5;                 average = static_cast <float> (sum) / 5;                 x = x + average;         }         classAve = x / students;         return classAve; } void calculateGrade(int &test1, int &test2, int &test3, int &test4, int &test5, int &countA, int &countB, int &countC, int &countD, int &countF, int &students, string &student, float &average) {         float grade;         for (int i = 1; i <= students; i++)         {                 grade = average;                 if ((90 <= grade) && (grade <= 100))                         countA++;                 else if ((80 <= grade) && (grade <= 89.99))                         countB++;                 else if ((70 <= grade) && (grade <= 79.99))                         countC++;                 else if ((60 <= grade) && (grade <= 69.99))                         countD++;                 else if ((0 <= grade) && (grade <= 59.99))                         countF++;         } }```
The problem I'm having is that it only takes the last person's grade and does the calculations with those numbers.

Example:

infile:
bob 88 96 85 76 99
suzy 91 84 72 94 74
johnney 100 98 79 93 86

outfile:
johnney 100 98 79 93 86
johnney 100 98 79 93 86
johnney 100 98 79 93 86
A appeared 3 times.
B appeared 0 times.
C appeared 0 times.
D appeared 0 times.
F appeared 0 times.

In reality, Bob and Suzy got B's. I narrowed it down to there being something wrong with my loop, but I can't figure it out.

Any help would be appreciate it. Thanks in advance!
• 11-15-2008
laserlight
A few things to note:
• void main() should be int main()
• You should not use inData.eof() to control the loop.
• Instead of test1, test2, etc, you should have an array of arrays, or better yet, design a student class that models a student with test grades, then store an array (or rather, a vector) of students. The failure to do this is the reason why only the grade of the last student are computed.
• calculateAverage() does not appear to need to take any variables by reference.
• calculateGrade() appears to have several variables that are unused.
• 11-15-2008
syrup890
I tried using arrays and it just wasn't working. I believe it wouldn't read the data for some reason. I was getting frustrated; I asked the professor and he said the problem can be solved without arrays anyway, so that's what I was trying to do here. And we haven't gotten into vectors yet. It's a pretty basic C++ class.

I do appreciate the other comments though. I'll make those changes, but I consider them minor compared to the original problem. I need to figure out a way to have it read all the students first.
• 11-15-2008
laserlight
Quote:

Originally Posted by syrup890
I tried using arrays and it just wasn't working. I believe it wouldn't read the data for some reason. I was getting frustrated; I asked the professor and he said the problem can be solved without arrays anyway, so that's what I was trying to do here.

Ah yes, that would actually be a more space efficient solution. This means that you need to keep a running total for the overall grade points, as well as running totals for each alphabetic grade. Oh, and you need a running total of the number of students in order to compute the overall average, but you already have such a variable.

As soon as you read the data for a student, add each numeric grade to the overall running total. Then compute the average of the student's grades and print it out. Use this average to determine the alphabetic grade of the student, and increment the corresponding running total.

After processing the data for all the students, compute the overall average and print it along with the running totals of the alphabetic grades.