Write a C program that reads from a formatted file a list of
Write a C++ program that reads from a formatted file a list of 6 students and their grades for 3 tests, computes the average test score for each student and the class and the overall average for each test and for all tests and outputs them in a file (in a special format).
The formatted input file contains on each line the name, student ID, and the grades for the 4
tests for a student (one line per student).
The format of the input file is the following:
Student1FirstName Student1LastName Student1ID Student1Test1 Student1Test2 Student1Test3
Student2FirstName Student2LastName Student2ID Student2Test1 Student2Test2 Student2Test3
Student3FirstName Student3LastName Student3ID Student3Test1 Student3Test2 Student3Test3
Student4FirstName Student4LastName Student4ID Student4Test1 Student4Test2 Student4Test3
Student5FirstName Student5LastName Student4ID Student4Test1 Student4Test2 Student4Test3
Student6FirstName Student6LastName Student4ID Student4Test1 Student4Test2 Student4Test3
The output file should be formatted as following:
STUDENTS
Student1LastName, Student1FirstNameInitial. Student1ID Student1TestAverage
Student2LastName, Student1FirstNameInitial. Student2ID Student2TestAverage
Student3LastName, Student1FirstNameInitial. Student3ID Student3TestAverage
Student4LastName, Student1FirstNameInitial. Student4ID Student4TestAverage
Class ClassAverage
TESTS
Test 1 Test1Average
Test 2 Test2Average
Test 3 Test3Average
Tests TestsAverage
where:
§StudentXLastName is the last name of student X as read from the input file.
§StudentXFirstNameInitial is the initial (first letter) of the first name of student X that you read
from the input file.
§StudentXID is the student X’s ID as read from the input file
§StudentXTestAverage is the average between the student X\'s grades for Test1, Test2, and
Test3.
§ClassAverage is the average of the student test averages.
§TestXAverage is the average of the student X\'s grades for Test1, Test2, and Test3.
§TestsAverage is the average of the test averages.
§The reminder values are constants and they should be outputted as they are.The students and tests averages and overall totals should have exactly 2 decimals and should be
aligned to the right. The rest of the values should be aligned to the left. Make sure you are not missing any of the digits of the student ID.
If you are unable to determine the initial of the first name, you should use the first name instead
You should prepare your own input file according to the input file format above by randomly
chose students’ names and test grades.
Solution
Here is the code for you:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string firstName, lastName;
int studentID, test1, test2, test3;
double studentAverage, classAverage, test1Avg, test2Avg, test3Avg, classAvg;
string fileName;
cout<<\"Enter the name of the input file: \";
cin>>fileName;
ifstream fin;
fin.open(fileName);
cout<<\"Enter the name of the output file: \";
cin>>fileName;
ofstream fout;
fout.open(fileName);
test1Avg = test2Avg = test3Avg = classAvg = 0;
fout<<\"STUDENTS\"<<endl;
for(int i = 0; i < 6; i++)
{
fin>>firstName>>lastName>>studentID>>test1>>test2>>test3;
double stuAvg = (test1 + test2 + test3) / 3.0;
classAvg += stuAvg;
fout<<lastName<<firstName.at(0)<<studentID<<stuAvg<<endl;
test1Avg += test1;
test2Avg += test2;
test3Avg += test3;
}
fout<<\"Class\"<<classAvg / 6.0<<endl;
fout<<\"TESTS\"<<endl;
cout<<\"Test 1\"<<test1Avg/6.0<<endl;
cout<<\"Test 2\"<<test2Avg/6.0<<endl;
cout<<\"Test 3\"<<test3Avg/6.0<<endl;
cout<<\"Tests\"<<(test1 + test2 + test3) / 18.0<<endl;
}

