Getting average problem

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!

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
//  main.cpp
//  Program 8
//  Created by William Blake Harp on 7/16/14.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    double average = 0;
    double class_average = 0;
    int sum_of_test_scores = 0;
    
    //2D Array.
    const int students = 5;
    const int test_scores = 4;
    int scores[students][test_scores];
    
    //Arrays on the heap.
    string* arrayNames = nullptr;
    arrayNames = new string[students];
    
    int* arrayID = nullptr;
    arrayID = new int[students];
    
    int* arrayAverage = nullptr;
    arrayAverage = new int[students];
    
    int* arrayLetterGrade = nullptr;
    arrayLetterGrade = new int[students];
    
    ifstream inputFile;
    
    // Open the file.
    inputFile.open("Student Data.txt");
    
    // do an initial test.
    cout<<boolalpha;
    cout<< "Is the file good? --> "<<inputFile.good()<<endl<<endl;
    
    if (inputFile.fail())
        cout << "Can't open the file!" << endl;
    
    else
    {
        for( int i(0); i != 5; i++ )
        {
            // read name.
            getline(inputFile, arrayNames[i]);
            cout<<arrayNames[i]<<endl;
            
            // read student id.
            inputFile >> arrayID[i];
            cout<< arrayID[i] <<endl;
            
            // read four(4) scores.
            for( int j(0); j != 4; j++ )
            {
                inputFile >> scores[i][j];
                cout << scores[i][j] << endl;
            }
            
            string str;
            
            // consume '\n' char (Use getline for mac/Xcode!!!).
            getline (inputFile, str);
            
            
            cout << "I have read "<<i+1 <<" record/s\n\n";
        }// end read loop.
        
        // display student name, score, ID and average.
        for( int i(0); i != 5; i++ )
        {
            cout << arrayNames[i];
            
            for( int j(0); j != 4; j++ )
            {
                cout << scores[i][j] << " ";
                
                sum_of_test_scores += scores[i][j];
            }
            cout << endl;
            
            average = sum_of_test_scores / 5.0;
            cout << "The average is: " << average << endl;
            
            if (average > 100)
            {
                cout << "invalid average for grade please try again." << endl;
            }
            else if(average >= 90 && average <= 100)
            {
                cout << "Letter grade is A!" << endl;
            }
            else if (average >= 89 && average <= 80)
            {
                cout << "Letter grade is B" << endl;
            }
            else if (average >= 79 && average <= 70)
            {
                cout << "Letter grade is C" << endl;
            }
            else if (average >= 79 && average <= 60)
            {
                cout << "Letter grade is D" << endl;
            }
            else
            {
                cout << "Letter grade is F!" << endl;
            }

            cout << endl;
            
        }// End display name & scores.
        
        cout << endl;
        
        // Loop for class average.
        for (int i = 0; i != 5; i++)
        {
            class_average = sum_of_test_scores / 5.0;
            
        }
        cout << "The class average is: " << class_average << endl;
        
        // Close the file.
        inputFile.close();
        
        // releasing memory block on the heap.
        delete [] arrayNames;
        arrayNames = 0;
        delete [] arrayID;
        arrayID = 0;
        delete [] arrayAverage;
        arrayAverage = 0;
        delete [] arrayLetterGrade;
        arrayLetterGrade = 0;
    }
    return 0;
}// End Code. 



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
Last edited on
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
how would you reset it to 0? Could you show me?
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.

Topic archived. No new replies allowed.