Write a C++ program that reads students\' names followed by their test scores. T
ID: 3675720 • Letter: W
Question
Write a C++ program that reads students' names followed by their test scores. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade.
Student data should be stores in a struct variable of type studentType, which has four components: studentFName and studentLname of type string, testScore of type int (testScore is between 0 and 100), and and grade of type char. Suppose that the class has 20 students. Use an array of 20 components of type studentType.
Your Program must contain at least the following functions:
a. A function to read the students' data into the array.
b. A function to assign the relevant grade to each student.
Your program must output each students' name in this form: last name followed by a comma, followed by a space, followed by the first name; the name must be left justified. Moreover, other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls.
Read data from this file: C: est.txt (the file contents shown below)
Mike Smith 100 97 95 94 93
Tommy King 97 97 93 94 90
Phillip Franklin 97 93 97 93 92
Anthony Dowling 97 95 92 93 94
Anna West 92 93 93 94 93
Betsy Ross 95 94 97 93 92
Cindy Brady 93 94 94 95 93
Deanna Dee 7 96 95 94 95
Erik Fredricks 97 97 94 95 94
Fawn Taylor 97 94 93 91 90
Greg Gregory 93 94 92 95 93
Hanna Montanna 93 97 97 93 92
Iris Flowers 93 94 95 94 95
Jack Jackson 92 93 94 95 93
Kim Terry 93 94 93 92 91
Lorrie Haynes 93 97 97 94 97
Marsha Mason 97 97 94 93 92
Ned Lawson 97 93 97 94 91
Opie Taylor 94 91 90 97 94
Peter Briggs 95 94 92 90 90
Explanation / Answer
Please find the required code below :
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct studentType
{
string studentFName;
string studentLName;
char grade;
int testScore;
};
char getGrade(int score){
char g;
if(score > 80)
g = 'A';
else if(score > 60)
g = 'B';
else if(score > 50)
g = 'C';
else if(score > 40)
g = 'D';
else
g = 'F';
return g;
}
studentType * readData(){
studentType students[20];
string arr[7];
int i = 0;
ifstream input( "filename.ext" );
for( std::string line; getline( input, line ); )
{
stringstream ssin(line);
while (ssin.good() && i < 7){
ssin >> arr[i];
++i;
}
struct studentType s;
s.studentFName = arr[0];
s.studentLName = arr[1];
s.testScore = arr[2];
s.grade = getGrade(s.testScore);
students[i] = s;
}
return students;
}
int main( )
{
studentType * students;/
students = readData();
ofstream myfile;
myfile.open ("output.txt");
for(int i=0; i<20; i++){
myfile << students[i].studentLName <<", " << students[i].studentFName << " " << students[i].grade << endl;;
}
myfile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.