// Header files
#include <iostream>
#include<string>
#include<iomanip>
// Using namespace standard
usingnamespace std;
// Structure student
struct student
{
string SFname;
string SLname;
int Sid;
double sgrade[4]; // Hold four grades only
double fgrade; // Final grade score
char lgrade; // Letter grade
};
// Function header
void input(student Students[]); // X is passed by reference
void output(student Students[], double);
void finalGrade(student Students[]);
void letterGrade(student Students[]);
double bestStudent(student Students[]);
// Main
int main()
{
// Variables declared.
char highest;
double highestScore;
// Constant declared and initialized.
constint MAX_STUDENTS = 10;
// Object declared
student Students[MAX_STUDENTS];
// 10 function calls
input(Students); // When students is passed, it passes the array.
// Final grade calculator.
finalGrade(Students);
// Letter grade
letterGrade(Students);
// Best student
highestScore = bestStudent(Students);
// Output everything
output(Students, highestScore);
return 0;
}
// Function
void input(student Students[]) // X is passed by reference
{
int x = 0;
// Enter data for ten students.
for (x = 0; x < 10; x++)
{
cout << "Please enter Students first name ";
cin >> Students[x].SFname;
cout << "Please enter Students last name ";
cin >> Students[x].SLname;
cout << "Please enter the Students ID ";
cin >> Students[x].Sid;
cout << "Please enter the students first grade ";
cin >> Students[x].sgrade[0];
cout << "Please enter the students second grade ";
cin >> Students[x].sgrade[1];
cout << "Please enter the students third grade ";
cin >> Students[x].sgrade[2];
cout << "Please enter the students fourth grade ";
cin >> Students[x].sgrade[3];
cout << endl << endl;
}
}
// Display function
void output(student Students[], double highestValue)
{
// Local variables
int i,
j,
k;
// Display passing students.
cout << "PASS" << endl;
cout << "--------------------------------------------------------------" << endl;
for (int i = 0; i < 10; i++)
{
if (Students[i].fgrade > 59)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[i].SLname << " , "
<< Students[i].SFname << setw(30) << right
<< Students[i].Sid << setw(30) << right << Students[i].lgrade << endl << endl;
}
}
// Displays failing students
cout << "FAIL" << endl;
cout << "--------------------------------------------------------------" << endl;
for (int j = 0; j < 10; j++)
{
if (Students[i].fgrade < 60)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[i].SLname << " , "
<< Students[i].SFname << setw(30) << right
<< Students[i].Sid << setw(30) << right << Students[i].lgrade << endl << endl;
}
}
// Who had the highest score.
cout << "HIGHEST SCORE" << endl;
cout << "--------------------------------------------------------------" << endl;
for (k = 0; k < 10; k++)
{
if(Students[i].fgrade == highestValue)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[i].SLname << " , "
<< Students[i].SFname << setw(30) << right
<< Students[i].Sid << setw(30) << right << Students[i].lgrade << endl << endl;
}
}
}
// Final grade function
void finalGrade(student Students[])
{
// Local variables.
int i;
double totalVar = 0; // Initialized to zero
// For loop to determine final grades.
for (i = 0; i < 10; i++)
{
// Temporarily store the students grade percentage total in a variable.
totalVar = Students[i].sgrade[0] + Students[i].sgrade[1] + Students[i].sgrade[2] + Students[i].sgrade[3];
Students[i].fgrade = totalVar / 4; // Average grade is the final grade
}
}
// Letter grade function
void letterGrade(student Students[])
{
// Local variables
int i;
// For loop goes through all the students and assigns a grade.
for (i = 0; i < 10; i++)
{
// If...else if statement that determines the grade of students.
if (Students[i].fgrade <= 100 && Students[i].fgrade >= 90)
Students[i].lgrade = 'A';
elseif (Students[i].fgrade >= 80 && Students[i].fgrade <= 89)
Students[i].lgrade = 'B';
elseif (Students[i].fgrade >= 70 && Students[i].fgrade <= 79)
Students[i].lgrade = 'C';
elseif (Students[i].fgrade >= 60 && Students[i].fgrade <= 69)
Students[i].lgrade = 'D';
elseif (Students[i].fgrade >= 0 && Students[i].fgrade <= 59)
Students[i].lgrade = 'F';
}
}
// Best student function
double bestStudent(student Students[])
{
// Local variables
int i;
// Separated to initialize
double highScore = Students[0].fgrade; // Set the highest score to the first element in the array.
// For loop to determine who has the highest score.
for (i = 0; i < 10; i++)
{
// Determines if the next element is higher.
if (Students[i].fgrade > highScore)
highScore = Students[i].fgrade; // If it is higher, then it assigns to highScore.
}
// Returns highest score.
return highScore;
}
double fgrade = 0.0;
int x = 0;
//double fgrade;
student Students[10];
input(Students, x);
Calc(Students);
gradecalc(Students);
bestStudent(Students);
cin.get(); cin.get();
return 0;
}
void input(student Students[], int &x){
for (int x = 0; x < 10; x++) {
cout << "Please enter Students first name" << endl;
cin >> Students[x].SFname;
cout << "Please enter Students last name" << endl;
cin >> Students[x].SLname;
cout << "Please enter the Students ID" << endl;
cin >> Students[x].Sid;
cout << "Please enter the students first grade" << endl;
cin >> Students[x].sgrade[0];
cout << "Please enter the students second grade" << endl;
cin >> Students[x].sgrade[1];
cout << "Please enter the students third grade" << endl;
cin >> Students[x].sgrade[2];
cout << "Please enter the students fourth grade" << endl;
cin >> Students[x].sgrade[3];
}
}
void Calc(student Students[]){
double grade=0.0;
double total;
total = 0;
for (int x = 0; x < 10; x++) {
total = Students[x].sgrade[0] + Students[x].sgrade[1] + Students[x].sgrade[2] + Students[x].sgrade[3];
Students[x].fgrade = total / 4;
}
return ;
}
void output(student Students[], double highestValue)
{
// Local variables
int x,
y,
z;
// Display passing students.
cout << "PASS" << endl;
cout << "--------------------------------------------------------------" << endl;
for (int x = 0; x < 10; x++)
{
if (Students[x].fgrade > 59)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
}
}
// Displays failing students
cout << "FAIL" << endl;
cout << "--------------------------------------------------------------" << endl;
for (int y = 0; y < 10; y++)
{
if (Students[x].fgrade < 60)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
}
}
cout << "HIGHEST SCORE" << endl;
cout << "--------------------------------------------------------------" << endl;
for (z = 0; z < 10; z++)
{
if (Students[x].fgrade == highestValue)
The only issue with my code right now is getting the output to align into a table which is a quick fix. It compiles perfectly fine, but the letter is not showing up as it is assigned. I just typed in an F letter grade and got the letter alpha back.
cin.get(); cin.get();
return 0;
}
void input(student Students[], int &x){
for (int x = 0; x < 10; x++) {
cout << "Please enter Students first name" << endl;
cin >> Students[x].SFname;
cout << "Please enter Students last name" << endl;
cin >> Students[x].SLname;
cout << "Please enter the Students ID" << endl;
cin >> Students[x].Sid;
cout << "Please enter the students first grade" << endl;
cin >> Students[x].sgrade[0];
cout << "Please enter the students second grade" << endl;
cin >> Students[x].sgrade[1];
cout << "Please enter the students third grade" << endl;
cin >> Students[x].sgrade[2];
cout << "Please enter the students fourth grade" << endl;
cin >> Students[x].sgrade[3];
}
}
void Calc(student Students[]){
double grade=0.0;
double total;
total = 0;
for (int x = 0; x < 10; x++) {
total = Students[x].sgrade[0] + Students[x].sgrade[1] + Students[x].sgrade[2] + Students[x].sgrade[3];
cin.get(); cin.get();
return 0;
}
void input(student Students[], int &x){
for (int x = 0; x < 10; x++) {
cout << "Please enter Students first name" << endl;
cin >> Students[x].SFname;
cout << "Please enter Students last name" << endl;
cin >> Students[x].SLname;
cout << "Please enter the Students ID" << endl;
cin >> Students[x].Sid;
cout << "Please enter the students first grade" << endl;
cin >> Students[x].sgrade[0];
cout << "Please enter the students second grade" << endl;
cin >> Students[x].sgrade[1];
cout << "Please enter the students third grade" << endl;
cin >> Students[x].sgrade[2];
cout << "Please enter the students fourth grade" << endl;
cin >> Students[x].sgrade[3];
}
}
void Calc(student Students[]){
double grade=0.0;
double total;
total = 0;
for (int x = 0; x < 10; x++) {
total = Students[x].sgrade[0] + Students[x].sgrade[1] + Students[x].sgrade[2] + Students[x].sgrade[3];
I got it to compile so that is good but the output i all messed up also the bestStudent function seems to not be working as it is not picking which student has the highest score
@OP So extract the offending function, make up some test data instead of all the menu and typing and write a short test program that can be pasted back in. When you've done that show us the program, test data and the results and maybe help is possible. Don't forget proper code tags as requested before.