Print average score

Hi, so I've done a homework but I have still a couple problems.

The goal is: "Write a program that prompts to read students' names followed by their test scores (1 test score for every student). The program should output to the screen, the student's name followed by the test score, and the relevant grade. At the end, it should also find and print the highest test score and the name of the student with the highest test score, the lowest test score and the name of the student with the lowest test score, and the average of the test scores."

Here's my code:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct StudentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};

const int MAXSTUDENTS = 5;
void display(StudentType students[], int nStudents);
int findHighest(StudentType students[], int nStudents);
int findLowest(StudentType students[], int nStudents);
char letGrades(int grade);
void calGrades(StudentType students[], int nStudents);
void topScores(int highestScore, StudentType students[], int nStudents);

int main()
{
int studentCount = 0;
StudentType students[MAXSTUDENTS];
cout << "Enter the student's first name, last name, and test score. " << endl;
cin >> students[studentCount].studentFName;
cin >> students[studentCount].studentLName;
cin >> students[studentCount].testScore;
studentCount++;

char input;
cout << "Would you like to enter another student?(y or n): ";
cin >> input;
//cin.ignore('\n');
//cin.clear();

while (input == 'y')
{
cout << "Enter the student's first name, last name, and test score. " << endl;
cin >> students[studentCount].studentFName;
cin >> students[studentCount].studentLName;
cin >> students[studentCount].testScore;
studentCount++;

cout << "Would you like to enter another student?(y or n): ";
cin >> input;
//cin.ignore('\n');
//cin.clear();
if (input == 'y' && studentCount >= MAXSTUDENTS)
{
cout << "Sorry, you can't put any more students." << endl;
input = 'n';
}


}
calGrades(students, studentCount);
display(students, studentCount);

int highest = findHighest(students, studentCount);
cout << "Highest score is: " << students[highest].testScore << endl;
cout << "Name : " << students[highest].studentFName << " " << students[highest].studentLName << endl;

int lowest = findLowest(students, studentCount);
cout << "lowest score is: " << students[lowest].testScore << endl;
cout << "Name : " << students[lowest].studentFName << " " << students[lowest].studentLName << endl;

return 0;
}

void calGrades(StudentType students[], const int nStudents)
{
int i;
for (i = 0; i < nStudents; i++)
{
students[i].grade = letGrades(students[i].testScore);
}
}

int findHighest(StudentType students[], const int nStudents)
{
int highest = 0;
for (int i = 1; i < nStudents; i++)
{
if (students[highest].testScore < students[i].testScore)
highest = i;
}
return highest;
}

void display(StudentType students[], int nStudents)
{
int i;
for (i = 0; i < nStudents; i++)
{
cout << "Name: " << students[i].studentFName << " " << students[i].studentLName << endl;
cout << "Score: " << students[i].testScore << endl;
cout << "Grade: " << students[i].grade << endl << endl;
}
}

int findLowest(StudentType students[], int nStudents)
{
int lowest = 0;
for (int i = 1; i < nStudents; i++)
{
if (students[lowest].testScore > students[i].testScore)
lowest = i;
}
return lowest;
}

void topScores(int highestScore, StudentType students[], const int nStudents)
{
int i;
cout << highestScore << letGrades << " is the highest score within a group of " << nStudents << " students." << endl;
for (i = 0; i < nStudents; i++)
{
if (students[i].testScore == highestScore)
{
cout << students[i].studentLName << ", " << students[i].studentFName << endl;
}
}
}

char letGrades(int grade)
{
char charGrades;
if (grade >= 90)
{
charGrades = 'A';
}
else if (grade >= 80)
{
charGrades = 'B';
}
else if (grade >= 70)
{
charGrades = 'C';
}
else if (grade >= 60)
{
charGrades = 'D';
}
else
{
charGrades = 'F';
}
return charGrades;
}

It's perfectly working so far, but the thing is I also have to put the average of all the scores as well as all left justified the outputs. How do I do it?
Last edited on
Please, for love of all things sane, use the code tags.

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct StudentType
{
    string studentFName;
    string studentLName;
    int testScore;
    char grade;
};
const int MAXSTUDENTS = 5;
void display(StudentType students[], int nStudents);
int findHighest(StudentType students[], int nStudents);
int findLowest(StudentType students[], int nStudents);
char letGrades(int grade);
void calGrades(StudentType students[], int nStudents);
void topScores(int highestScore, StudentType students[], int nStudents);
int main()
{
    int studentCount = 0;
    StudentType students[MAXSTUDENTS];
    cout << "Enter the student's first name, last name, and test score. " << endl;
    cin >> students[studentCount].studentFName;
    cin >> students[studentCount].studentLName;
    cin >> students[studentCount].testScore;
    studentCount++;
    char input;
    cout << "Would you like to enter another student?(y or n): ";
    cin >> input;
    //cin.ignore('n');
    //cin.clear();
    while (input == 'y')
    {
        cout << "Enter the student's first name, last name, and test score. " << endl;
        cin >> students[studentCount].studentFName;
        cin >> students[studentCount].studentLName;
        cin >> students[studentCount].testScore;
        studentCount++;
        cout << "Would you like to enter another student?(y or n): ";
        cin >> input;
        //cin.ignore('n');
        //cin.clear();
        if (input == 'y' && studentCount >= MAXSTUDENTS)
        {
            cout << "Sorry, you can't put any more students." << endl;
            input = 'n';
        }
    }
    calGrades(students, studentCount);
    display(students, studentCount);
    int highest = findHighest(students, studentCount);
    cout << "Highest score is: " << students[highest].testScore << endl;
    cout << "Name : " << students[highest].studentFName << " " << students[highest].studentLName << endl;
    int lowest = findLowest(students, studentCount);
    cout << "lowest score is: " << students[lowest].testScore << endl;
    cout << "Name : " << students[lowest].studentFName << " " << students[lowest].studentLName << endl;
    return 0;
}
void calGrades(StudentType students[], const int nStudents)
{
    int i;
    for (i = 0; i < nStudents; i++)
    {
        students[i].grade = letGrades(students[i].testScore);
    }
}
int findHighest(StudentType students[], const int nStudents)
{
    int highest = 0;
    for (int i = 1; i < nStudents; i++)
    {
        if (students[highest].testScore < students[i].testScore)
        highest = i;
    }
    return highest;
}
void display(StudentType students[], int nStudents)
{
    int i;
    for (i = 0; i < nStudents; i++)
    {
        cout << "Name: " << students[i].studentFName << " " << students[i].studentLName << endl;
        cout << "Score: " << students[i].testScore << endl;
        cout << "Grade: " << students[i].grade << endl << endl;
    }
}
int findLowest(StudentType students[], int nStudents)
{
    int lowest = 0;
    for (int i = 1; i < nStudents; i++)
    {
        if (students[lowest].testScore > students[i].testScore)
        lowest = i;
    }
    return lowest;
}
void topScores(int highestScore, StudentType students[], const int nStudents)
{
    int i;
    cout << highestScore << letGrades << " is the highest score within a group of " << nStudents << " students." << endl;
    for (i = 0; i < nStudents; i++)
    {
        if (students[i].testScore == highestScore)
        {
            cout << students[i].studentLName << ", " << students[i].studentFName << endl;
        }
    }
}
char letGrades(int grade)
{
    char charGrades;
    if (grade >= 90)
    {
        charGrades = 'A';
    }
    else if (grade >= 80)
    {
        charGrades = 'B';
    }
    else if (grade >= 70)
    {
        charGrades = 'C';
    }
    else if (grade >= 60)
    {
        charGrades = 'D';
    }
    else
    {
        charGrades = 'F';
    }
    return charGrades;
}


I went ahead and "prettified" your code for you. Please keep your code nicely formatted.

You can use iomanip to do the formatting.
Here's a page that lists all the formatting it can do: http://www.cplusplus.com/reference/ios/ios_base/fmtflags/
If you want to calculate the average, you'll have to add up all the scores and divide by number of students.
Last edited on
sorry for the code tag, and I solved the all left justified.

but for the avr, which one is my number of students?
It seems you do this every time a student is added studentCount++;
It looks like you're storing that value in that variable.
I did it! Thanks !
Glad you solved it!
Topic archived. No new replies allowed.