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."
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';
}
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;
}
}
}
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?
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
struct StudentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};
constint 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[], constint nStudents)
{
int i;
for (i = 0; i < nStudents; i++)
{
students[i].grade = letGrades(students[i].testScore);
}
}
int findHighest(StudentType students[], constint 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[], constint 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';
}
elseif (grade >= 80)
{
charGrades = 'B';
}
elseif (grade >= 70)
{
charGrades = 'C';
}
elseif (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.