Hello, I am having a bit of a beginner problem :) In my code my average is not coming out to be right. Bellow is my code and the out put. Thanks for the help!
Is the file good? --> true
Amy Adams
10111
97
86
78
95
I have read 1 record/s
Ben Barr
20222
89
81
73
87
I have read 2 record/s
Carla Carr
30333
79
71
63
77
I have read 3 record/s
Don Davis
40444
69
62
58
67
I have read 4 record/s
Edna Eaton
50555
63
51
62
48
I have read 5 record/s
Amy Adams
97 86 78 95
The average is: 71.2
Letter grade is F!
Ben Barr
89 81 73 87
The average is: 137.2
invalid average for grade please try again.
Carla Carr
79 71 63 77
The average is: 195.2
invalid average for grade please try again.
Don Davis
69 62 58 67
The average is: 246.4
invalid average for grade please try again.
Edna Eaton
63 51 62 48
The average is: 291.2
invalid average for grade please try again.
The class average is: 291.2
This is one reason magic number are bad average = sum_of_test_scores / 5.0;
should be average = sum_of_test_scores / test_scores;
then you would not make the mistake of putting the wrong number for the divisor.
Also sum_of_test_scores is not reset to 0 so it just keeps adding up all the scores.
((97 + 86 + 78 + 95) + (89 + 81 + 73 + 87)) / 5.0 = 137.2
sum_of_test_scores = 0;
I would do it after the average is calculated. You are also going to need another variable to keep a running total of all the scores in order to get the class average. Obviously you need to add the sum to that variable before resetting it also.