Baccessal1/Tesing file C No description Write aC+ program using Replit.com onlin
ID: 3720458 • Letter: B
Question
Baccessal1/Tesing file C No description Write aC+ program using Replit.com online (use the example provided to get an idea of what the program should look like). The program that will read student data from a file, compute average test scores and grades for the course, and output the data in a file. The data that will be stored in the output file are the student names, average test scores and grades only for the students that have a grade of C or better. Files history D main.cpp data.tx 1 #include?iostrear 2 #includeExplanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct student{
string firstName;
string lastName;
double avg;
double score[5];
char grade;
};
int readData(student data[]){
string first,last;
double a1,a2,a3,a4,a5;
ifstream fin("studentData.txt");
if (!fin){
cout << "Error opening file ";
return -1;
}
int count = 0;
while(fin >> first >> last >> a1 >> a2 >> a3 >> a4 >> a5){
data[count].firstName = first;
data[count].lastName = last;
data[count].score[0] = a1;
data[count].score[1] = a2;
data[count].score[2] = a3;
data[count].score[3] = a4;
data[count].score[4] = a5;
count++;
}
}
int calculateAvgAndGrade(student data[], int count){
double a1,a2,a3,a4,a5;
for (int i = 0; i<count; i++){
a1 = data[i].score[0];
a2 = data[i].score[1];
a3 = data[i].score[2];
a4 = data[i].score[3];
a5 = data[i].score[4];
data[i].avg = (a1+ a2 + a3 + a4 + a5)/5.0;
if (data[i].avg >= 90)
data[i].grade = 'A';
else if (data[i].avg >= 80)
data[i].grade = 'B';
else if (data[i].avg >= 70)
data[i].grade = 'C';
else
data[i].grade = ' ';
}
}
void display( student data[], int count){
cout << setw(20) << left << "Name" << setw(10) << "Average" << setw(10) << "Grade" << endl;
for (int i = 0; i<count; i++){
string name = data[i].firstName + " " + data[i].lastName;
cout << setw(20) << left << name << setw(10) << data[i].avg << setw(10) << data[i].grade << endl;
}
}
void writeToFile( student data[], int count){
ofstream fout("outputData.txt");
fout << setw(20) << left << "Name" << setw(10) << "Average" << setw(10) << "Grade" << endl;
for (int i = 0; i<count; i++){
string name = data[i].firstName + " " + data[i].lastName;
fout << setw(20) << left << name << setw(10) << data[i].avg << setw(10) << data[i].grade << endl;
}
fout.close();
}
int main(){
student data[1000];
int count = readData(data);
if (count != -1){
calculateAvgAndGrade(data,count);
display(data,count);
writeToFile(data,count);
}
else {
cout << "Error opening file ";
return 0;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.