Help calculating High, Low, Average for grades

The bottom three functions do not work. Nor are the finished to the extent.
I do not know how the data members are being accessed and is only giving me
the last output. The overall goal of this program is to output the highest grade with student name and age, lowest grade with student name and age, and the average of all grades. The file is given and is commented out for you to see what the file looks like.


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
// Header Files
#include <iostream>
#include <fstream>

using namespace std;

// Global Constants
const int STD_STR_LEN = 10;

// structs
struct student
   {
    char firstName[ STD_STR_LEN ];
    char lastName[ STD_STR_LEN ];
    int age;
    double gradePercentage;
   };

// Function Prototypes
void readFile( student &info );
void getHighestGrade( student &info );
void getLowestGrade( student &info );
void getAverageGrade( student &info );

// Main Function
int main()
   {
    // initialize variables
    student info;

    readFile( info );

    getLowestGrade( info );
    getHighestGrade( info );
    getAverageGrade( info );

    return 0;
   }

// Supporting Function Implementations
void readFile( student &info )
   {
    // initialize variables
    ifstream fin;
    int index; 

    fin.clear();
    fin.open( "students" );   

    // loop through file to max string length
    for( index = 0; index < STD_STR_LEN; index++ )
       {
        // acquire data for each statement
        fin >> info.firstName >> info.lastName >> info.age >> info.gradePercentage;
       }
   }

void getHighestGrade( student &info )
   {
    // initialize variables
    int index = 0;
    double percent = 0;
    double highGrade = 0;
    student sArray[ STD_STR_LEN ];

    for( index = 0; index < 10; index++ )
       {
        if( info.gradePercentage < highGrade )
           {
            highGrade = info.gradePercentage;
            highGrade++;
           } 
       } 
    cout << "Highest Grade: " << info.firstName << " " << info.lastName << " "
         << info.age << " " << highGrade << endl << endl; 
   }

void getLowestGrade( student &info )
   {
    // initialize variables
    double lowGrade;

    cout << "Lowest Grade: " << info.firstName << " " << info.lastName << " "
         << info.age << " " << lowGrade << endl << endl; 
   }

void getAverageGrade( student &info )
   {
    // initialize variables
    int index;
    double average;
    double total;
 
    for( index = 0; index < 10; index++ )
       {
        total += ( info.gradePercentage/10 );
       }

    cout <<  total << endl << endl; 
   }

//////////////////////////////////////////////////////
/////Student File ///////////////////////////////////
////////////////////////////////////////////////////
/*/////////////////////////////////////////////////
Frances 	Clark		22	89.70
Jason 		Davis		23	88.32
Michael 	Gallagher	23	98.36
Freddy		Hardy		18	64.87
Whitney 	Houston		21	72.65
Bob		Jones		22	97.36
Tom		Johnson		24	98.23
Sarah		Key		23	92.44
Josephine	Lacy		24	54.12	
Catherine	Smith		20	77.03		
////////////////////////////////////////////////*/
Last edited on
I'm sorry, but that answer has nothing to do with my question nor does that website even help in the least bit...
Last edited on
1
2
3
4
5
// loop through file to max string length
for( index = 0; index < STD_STR_LEN; index++ ) {
    // acquire data for each statement
    fin >> info.firstName >> info.lastName >> info.age >> info.gradePercentage;
}
This code will read first student in info. Then it will read second student in info, overwriting first. Then third, etc.
If you need to read many students, you either should read one at a time, do all operations with it, then read next... Or you need to read them in some container, like vector.
Your problem(s) start at line 29. You have only one student. On line 54 you overwrite that student STD_STR_LEN times, so you only keep the data of the last student.

You should have a list/vector/array of students and pass that to the functions. Assuming that the input file contains exactly STD_STR_LEN records is too static. You should learn to make generic and robust programs that lack such assumptions.

Using plain char arrays for the name fields is error-prone. You should use std::string instead.

Thanks for the help from both. Keskiverto, for the purposes of this project, we were not allowed to use the string library and for both, we are not allowed to use vectors. And for being early in the programming field, we have to assume a lot of things for now to learn what will happen.
Last edited on
The use of vector is not necessary, but "borrowing" ideas from it is useful. The vector has capacity (how many elements it can hold) and size (how many elements it has now). You can emulate that with an array and variables:
1
2
3
const size_t capacity = 100; // How many students we can handle at most
size_t size = 0; // How many students we have
student sinfo [capacity]; // the students 

When you read a new student from file, you store it into the next element in sinfo and increment the size by one. However, you can not read more than capacity lines. That is a simple check.

The other methods then iterate over size elements of the sinfo.
Topic archived. No new replies allowed.