In: Computer Science
C++
Description: You will write a program that reads students names followed by their final grade. It will output the names and their grade, followed by the corresponding letter grade. It will also print the name of the students(s) with the highest grade
Program code to copy:-
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//Declare global constants
const int SIZE = 20;
//Defining structure
struct studentType
{
string fname;
string lname;
double score;
char letterGrade;
};
//Function prototype
int readData(studentType stud[]);
void assignGrade(studentType stud[], int n);
void printStudentsInfo(studentType stud[], int n);
double findHighest(studentType stud[], int n);
void printHighest(studentType stud[], int n, double highest);
int main()
{
//Declare array to hold data read from
file
studentType stud[SIZE];
//Declare variable to hold number students
deatils read from file
int n;
//Calling function to read data from file into
structure array
n = readData(stud);
//Calling function to determine and assign grade
for each student
assignGrade(stud, n);
//Calling function to print students name, grade
and letter grade
printStudentsInfo(stud, n);
//Calling function to find the highest
grade
double highest = findHighest(stud, n);
//Calling function to print the names of
students with highest grade
printHighest(stud, n, highest);
}
//Function read first name, last name and grade from file into
structure array.
//Function takes reference to structure array as parameter
and
//returns number of students data read from file.
int readData(studentType stud[])
{
ifstream fin;
string filename;
//Prompt and read file name from user
cout << "Enter the input file name:
";
cin >> filename;
//Open the file for reading
fin.open(filename.c_str());
//Checking whether the file is opened
successfully or not
if(!fin)
{
cout << "File
failed to open for reading" << endl;
exit(0);
}
int i=0;
//Loop will be executed till end of file
while(!fin.eof())
{
//Reading each student
first name, last name and grade from file into structure
array.
fin >>
stud[i].fname >> stud[i].lname >> stud[i].score;
i++;
}
//Closing file
fin.close();
//Returns the number students deatils read from
file
return i;
}
//Function to determine and assign grade for each student.
//Function takes two parameters: reference to array of structure
and
//number of students.
void assignGrade(studentType stud[], int n)
{
//determine and assign grade for each
student
for(int i=0; i<n; i++)
{
if (stud[i].score >=
90)
stud[i].letterGrade = 'A';
else if (stud[i].score
>= 80)
stud[i].letterGrade = 'B';
else if (stud[i].score
>= 70)
stud[i].letterGrade = 'C';
else if (stud[i].score
>= 60)
stud[i].letterGrade = 'D';
else
stud[i].letterGrade = 'F';
}
}
//Function to print students name, grade and letter grade
//Function takes two parameters: reference to array of structure
and
//number of students.
void printStudentsInfo(studentType stud[], int n)
{
cout << endl;
cout << left << setw(15) <<
"First Name" << left << setw(15) << "Last
Name"
<< right
<< setw(7) << "Grade" << right << setw(20)
<< "Letter-Grade" << endl;
cout <<
"-----------------------------------------------------------"
<< endl;
cout << setprecision(1)
<<fixed;
for(int i=0; i<n; i++)
{
cout << left
<< setw(15) << stud[i].fname << left <<
setw(15) << stud[i].lname
<< right << setw(7) << stud[i].score <<
right << setw(15) << stud[i].letterGrade <<
endl;
}
cout <<
"-----------------------------------------------------------"
<< endl;
}
//Function to finds and returns the highest grade.
//Function takes two parameters: reference to array of structure
and
//number of students.
double findHighest(studentType stud[], int n)
{
double max = stud[0].score;
for(int i=1; i<n; i++)
{
if(stud[i].score >
max)
max = stud[i].score;
}
return max;
}
//Function to print the names of students with highest
grade.
//Function takes three parameters: reference to array of
structure,
//number of students and highest grade.
void printHighest(studentType stud[], int n, double highest)
{
cout << endl;
cout << "Name of students scored highest
grade:" << endl;
cout <<
"--------------------------------------" << endl;
for(int i=1; i<n; i++)
{
if(stud[i].score ==
highest)
cout << stud[i].fname << " " << stud[i].lname
<< endl;
}
}
Screenshot of output:-
Sample input file from where student first name, last name and grade read by the program:-
"StudentGrade.txt"
Daryl Dixon 89
Rick Grimes 92
Glenn Rhee 94
Maggie Greene 82
Carl Grimes 75
Carol Peletier 100
Beth Greene 87
Sasha Williams 79
Tara Chambler 81
Rosita Espinosa 90
Eugene Porter 100
Alfalfa Aloysius 40.0
Iker Casillas 65
Mesut Ozil 47
Xabi Alonso 100