Write a program that reads student names from a file followed by their test scor
ID: 3629853 • Letter: W
Question
Write a program that reads student names from a file followed by their test scores. The program should then output each student’s name followed by the test score and the relevant grade. It should also print the highest test score and the name of students having the highest test scores.
Specifically, write a code to:
•Read from the file (“inData.txt”) and store in a struct variable of type studentType, which has four components: studentFName, and studentLName of type string, testScore of type int (between the range of 0 and 100), and grade of type char.
•Suppose the class has 20 students. Use any array of 20 components of type studenetType.
•Your program should contain the definition of the following functions:
1.A function to read the students’ data into the array
void getData(ifstream& inFile, studentType sList[], int listSize);
2.A function to assign the relevant grade to each student
void calculateGrade(studentType sList[], int listSize);
3.A function to find the highest test score
int highestScore(const studentType sList[], int listSize);
4.A function to print the name of the students having the highest test score.
void printResult(const studentType sList[], int listSize);
Sample Input:
Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow Smitn 93
Alice Wonderful 89
Samina Akthar 85
Simba Green 95
Donald Egger 90
Brown Deer 86
Johny Jackson 95
Greg Gupta 75
Samuel Happy 80
Danny Arora 80
Sleepy June 70
Amy Cheng 83
Shelly Malik 95
Chelsea Tomek 95
Angela Clodfelter 95
Allison Nields 95
Lance Norman 88
Output:
Explanation / Answer
please rate - thanks
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct studentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};
void getData(ifstream& inFile, studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(const studentType sList[], int listSize);
int main()
{ifstream in;
in.open("inData.txt");
if(in.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
system("exit");
}
studentType sList[20];
getData(in,sList,20);
calculateGrade(sList,20);
printResult(sList,20);
in.close();
system("pause");
return 0;
}
void getData(ifstream& inFile,studentType sList[],int listSize)
{int n=0;
while(n<listSize)
{inFile>>sList[n].studentLName>>sList[n].studentFName>>sList[n].testScore;
n++;
}
}
void calculateGrade(studentType sList[], int listSize)
{int i;
for(i=0;i<listSize;i++)
if (sList[i].testScore<60)
sList[i].grade='F';
else if (sList[i].testScore<70)
sList[i].grade='D';
else if (sList[i].testScore<80)
sList[i].grade='C';
else if(sList[i].testScore<90)
sList[i].grade='B';
else
sList[i].grade='A';
}
int highestScore(const studentType sList[], int listSize)
{int high=0,i;
for(i=0;i<listSize;i++)
if (high < sList[i].testScore )
high=sList[i].testScore;
return high;
}
void printResult(const studentType sList[], int listSize)
{cout<<left<<setw(30)<<"Student Name"<<right<<setw(10)<<"TestScore"<<right<<setw(7)<<"Grade"<<endl;
string name;
int high,i;
for(i=0;i<listSize;i++)
{name=sList[i].studentLName+", "+sList[i].studentFName;
cout<<left<<setw(30)<<name<<right<<setw(10)<<sList[i].testScore<<right<<setw(7)<<sList[i].grade<<endl;
}
cout<<endl;
high=highestScore(sList, listSize);
cout<<"Highest Test Score: "<<high<<endl;
cout<<"Students having the highest test score: "<<endl;
for (int i=0; i < listSize; i++)
if (sList[i].testScore==high )
cout<<sList[i].studentLName<<", "<<sList[i].studentFName<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.