How to read the file and pass it through the arrays

I'm working on my second lab assignment and am not sure how to fill in the body of the code. I'm not even sure that the file is being read correctly. Please let me know what errors I am making and how I can improve on this code and meet the criteria set. Thank you so much for your time.

You are asked to write a program to collect data on students’ scores.
In an input file called studentScores.txt, you find information about 15 students. On each we have student’s last name(one word) followed by 3 test scores.

Option 1. Must call averageScorePerStudent function. You must also display a proper message if the name was not on the list.

Option 2. Must call averageScorePerStudent function.

Option 3. Must call maxScorePerAssignIndex function, and use that index in finding the student who made that score and display a message that looks like the following:
Please enter the assignment# (1-3): 1
Student Shriver has made the highest score of 98 on assignment #1.

Option 4. Must call lowerThanAverageList function. The lowerThanAverageList function itself MUST call overallAverage function as well as averageScorePerStudent function. The overallAverage function itself MUST also call averageScorePerStudent function!

Option 5. Must call maxScorePerStudent function



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
  #include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

const int numScores = 3;

//prototype functions
int averageScorePerStudent(int[], int);
int maxScorePerAssignIndex(int[][numScores], int, int);
int overallAverage(int[][numScores], int);
vector <string> lowerThanAverageList(int[][numScores], int, string[]);
int maxScoreIndexPerStudent(int[], int);




int main()
{
    const int Size = 15;
    int choice, scores[Size][numScores], Index, MaxScore, MaxIndex, Num;
    int grade1, grade2, grade3;
    string name, names[numScores];
    vector<string> UnderAvg;

    ifstream infile;
    infile.open("studentScores.txt");
    for (int i = 0; i < Size; i++)
        for (int j = 0; j < numScores; j++)
            infile >> scores[i][j];

    do
    {
        cout << "1. Display a student’s scores as well their average score: " << endl;
        cout << "2. What is the name of the student with the highest average score?" << endl;
        cout << "3. What is the name of the student with the highest score on a certain assignment:" << endl;
        cout << "4. Display the list of the students whose average score is lower than the overall average score." << endl;
        cout << "5. Display the names of all students, their max score, and the assignment that they made their maximum score on " << endl;
        cout << "6. Exit " << endl;
        cin >> choice;

        switch (choice)
        {
        case 1:
            cout << "Please enter the student's last name: " << endl;
            cin >> name;
            break;
        case 2:
            MaxIndex = 0;
            MaxScore = averageScorePerStudent(scores[0], numScores);

            break;
        case 3:
            cout << "Please enter the assignment # (1-3) " << endl;
            cin >> choice;
            break;
        case 4:
            break;
        case 5:
            break;
        }
    } while (choice != 6);
    cout << "Have a good day!" << endl;
    return 0;
}
int averageScorePerStudent(int score[], int num)
{
    int total = 0;
    for (int i = 0; i < num; i++)
        total += score[i];
    return total / num;
}
int maxScorePerAssignIndex(int score[][numScores], int num, int num2)
{
    int MaxScore = score[0][num2];
    int MaxIndex = 0;

    for (int i = 0; i < num; i++)
    {
        if (score[i][num2] > MaxScore)
        {
            MaxScore = score[i][num2];
            MaxIndex = i;
        }
    }
    return MaxIndex;
}
int overallAverage(int score[][numScores], int num)
{
    int total = 0;
    
    for (int i = 0; i < num; i++)
    {
        total += averageScorePerStudent(score[i], numScores);
    }
    return total / num;
}

vector <string> lowerThanAverageList(int score[][numScores], int num, string names[])
{
    vector<string> LowerThanAvg;
    int OverAvg = overallAverage(score, num);

    for (int i = 0; i < num; i++)
    {
        int AvgScore = averageScorePerStudent(score[i], numScores);

        if (AvgScore < OverAvg)
            LowerThanAvg.push_back(names[i]);
    }
    return LowerThanAvg;
}
int maxScoreIndexPerStudent(int score[], int num)
{
    int MaxIndex = 0;
    int MaxScore = score[0];

    for (int i = 1; i < num; i++)
    {
        if (score[i] > MaxScore)
        {
            MaxScore = score[i];
            MaxIndex = i;
        }
    }
    return MaxIndex;
}
I'm not even sure that the file is being read correctly.

You have to find out. If reading the file doesn't work all other code won't work either.
I would suggest to start with a simple main function to read and display the data. When you get this working focus on the next step and so on. Don't try to write to whole code at once.
No, the file isn't being read properly as the first element for each entry is a one-word name which isn't being read.

Between L30 and L31 you need to extract the name into names. Then this extra line and L31-32 will need to be a compound statement (with {} ).

I'd suggest adding an additional option to display all the data for all students. Only once that option works move on to the others.

PS. For further guidance it would be useful to post the data file.
Last edited on
Thank you, I ended up rewriting how the file was being read and worked on getting that read first.

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
#include <iostream>
#include <string>
#include <fstream>
#include <vector> 

using namespace std;  //learned that this always has to come before prototypes or else it tells you that everything is an ambigious symbol

const int numScores = 3;  //const int 3 to be used in arrays and in the main body of the file. 

//prototype functions
int averageScorePerStudent(int[], int);
int maxScorePerAssignIndex(int[][numScores], int, int);
int overallAverage(int[][numScores], int);
vector <string> lowerThanAverageList(int[][numScores], int, string[]);
int maxScoreIndexPerStudent(int[], int);




int main()
{
    const int Size = 15;
    int choice, scores[Size][numScores]; //scores array with 15 and 3
    int IndexCall, MaxScoreCall, MaxIndex, currentAvg, choice2; //these are placeholders for the function calls 
    string name, names[Size];  //string array names with 15
    vector<string> UnderAvg;

    ifstream infile; 
    infile.open("studentScores.txt");
    int i = 0;
    while (i < Size && infile >> names[i])
    {
        for (int j = 0; j < numScores; j++)  //using numScores instead of three to be easier to change later
            infile >> scores[i][j];
        i++;
    }
    infile.close();

    do  . 
    {
        cout << "1. Display a student’s scores as well their average score: " << endl;
        cout << "2. What is the name of the student with the highest average score?" << endl;
        cout << "3. What is the name of the student with the highest score on a certain assignment:" << endl;
        cout << "4. Display the list of the students whose average score is lower than the overall average score." << endl;
        cout << "5. Display the names of all students, their max score, and the assignment that they made their maximum score on " << endl;
        cout << "6. Exit " << endl;
        cin >> choice;

        switch (choice)
        {
        case 1:
            cout << "Please enter the student's last name: " << endl;
            cin >> name;
            
            for (int i = 0; i < Size; i++)
            {
                if (names[i] == name)
                    cout << names[i] << " average score : " << averageScorePerStudent(scores[i], numScores) << endl;
            }
            cout << "This is not a valid student name, please try again." << endl; // tried putting this with an else statement under the if statement, but would run continously and still show up even if the if statement is true. 
            break;
        case 2:
            MaxIndex = 0;
            MaxScoreCall = averageScorePerStudent(scores[0], numScores);
            for (int i = 0; i < Size; i++)
            {
                currentAvg = averageScorePerStudent(scores[i], numScores);
                if (currentAvg > MaxScoreCall)
                {
                    MaxScoreCall = currentAvg;
                    MaxIndex = i;

                }
            }
            cout << "Student with the highest average score: " << names[MaxIndex] << " - " << MaxScoreCall << endl;

            break;
        case 3:  //had dificulties not allowing the user to go beyond assignment 3. 
            cout << "Please enter the assignment # (1-3) " << endl;
            cin >> choice2; 
            IndexCall = maxScorePerAssignIndex(scores, Size, choice2);
            cout << "Student " << names[IndexCall] << " has made the highest score of " << scores[IndexCall][choice2] << " on assignment # " << choice2 << endl;
            break;
        case 4:
            UnderAvg = lowerThanAverageList(scores, Size, names);
            cout << "Students with average score less than overall average: " << endl;
            for (auto& names : UnderAvg)   
                cout << names << endl;
            cout << endl;
            break;
        case 5:
            for (int i = 0; i < Size; i++)
            {
                IndexCall = maxScoreIndexPerStudent(scores[i], numScores);
                cout << names[i] << " Max Score: " << scores[i][IndexCall] << " Assignment# " << IndexCall + 1 << endl; //adding plus one because it starts at 0 if not
            }
            cout << endl;
            break;
        }
    } while (choice != 6);
    cout << "Have a good day!" << endl;
    return 0;
}

int averageScorePerStudent(int scores[], int num)
{
    int total = 0;
    for (int i = 0; i < num; i++)
        total += scores[i];
    return total / num;
}
int maxScorePerAssignIndex(int scores[][numScores], int num, int num2)
{
    int MaxScore = scores[0][num2];
    int MaxIndex = 0;

    for (int i = 0; i < num; i++)
    {
        if (scores[i][num2] > MaxScore)
        {
            MaxScore = scores[i][num2];
            MaxIndex = i;
        }
    }
    return MaxIndex;
}
int overallAverage(int scores[][numScores], int num)
{
    int total = 0;
    
    for (int i = 0; i < num; i++)
    {
        total += averageScorePerStudent(scores[i], numScores);
    }
    return total / num;
}

vector <string> lowerThanAverageList(int scores[][numScores], int num, string names[])
{
    vector<string> LowerThanAvg;
    int OverAvg = overallAverage(scores, num);

    for (int i = 0; i < num; i++)
    {
        int AvgScore = averageScorePerStudent(scores[i], numScores);

        if (AvgScore < OverAvg)
            LowerThanAvg.push_back(names[i]);
    }
    return LowerThanAvg;
}
int maxScoreIndexPerStudent(int scores[], int num)
{
    int MaxIndex = 0;
    int MaxScore = scores[0];

    for (int i = 1; i < num; i++)
    {
        if (scores[i] > MaxScore)
        {
            MaxScore = scores[i];
            MaxIndex = i;
        }
    }
    return MaxIndex;
}


data file posted, sorry I didn't post it earlier with the original post.

Collins 89 80 78
Smith 90 79 80
Green 77 81 79
Johnson 68 70 72
Benson 90 86 88
Peterson 92 82 87
Ernest 93 88 78
Jackson 78 80 90
Jacobs 92 86 91
Donavon 82 80 77
Hoover 50 60 60
Black 94 83 81
Brown 81 77 79
Compton 79 72 68
Shriver 98 71 76
L30 i is the number of records read - which could be less than the max of 15. But at L55 and others you assume that exactly 15 records have been read. You should use the actual number of records read. What happens if the file only has 10 records?
Topic archived. No new replies allowed.