In: Computer Science
You are to write a program that grades students (Maybe I will use it on you). Your program must open and read in "input.txt" (an example of which is below). It will contain the students name and their test scores. You are to calculate the students final grade in the course. Take the average of their test scores and output their average to a file with the below format. For the output.txt ensure that the formatting is in the block style shown below. Test scores should always be shown to the second decimal place. input txt: (Meryl Streep 100 105 98.5 99.5 106)
output txt : (Meryl StreepTest scores:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Average score: .......................101.80Test score 1: ........................100.00Test score 2: ........................105.00Test score 3: .........................98.50Test score 4: .........................99.50Test score 5: ........................106.00~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~) Input fields: <First Name> <Last Name> <Score on Test 1> <Score on Test 2> <Score on Test 3> <Score on Test 4> <Score on Test 5>
Please use below code. In case of any query, do comment:
#include <iostream>
#include<iomanip> //used for setw and setprecesion
#include <fstream> //used for file handling
using namespace std;
int main()
{
//file stream handle for input file
ifstream inputFile;
//file stram handle for output file
ofstream outputFile;
//variable to read first name and last name
string firstname, lastname;
//for reading scores
float score1, score2, score3,score4,score5;
//average score to calculate
float averageScore =0.0;
//open the input and output files
inputFile.open("input.txt");
outputFile.open("output.txt");
//read the input file line by line till end of file
while(!inputFile.eof())
{
//read the variable from inout file
inputFile >> firstname >> lastname >> score1 >> score2 >> score3 >> score4 >> score5;
//calculate the average Score
averageScore = (score1 + score2 + score3 + score4 + score5)/5;
//output the expected formatted output to the output file
outputFile << "(" <<firstname << " " << lastname << " Test Scores: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Average Score:";
//std::Fixed to set the fixed width of the output and setprecison(2) is used to display two decimal places in the scores
outputFile <<"......................." << std::fixed << std::setprecision(2) << averageScore;
outputFile <<" Test score 1: ........................" << std::fixed <<std::setprecision(2) << score1;
outputFile <<" Test score 2: ........................" << std::fixed <<std::setprecision(2) << score2;
outputFile <<" Test score 3: ........................" << std::fixed <<std::setprecision(2) << score3;
outputFile <<" Test score 4: ........................" << std::fixed <<std::setprecision(2) << score4;
outputFile <<" Test score 5: ........................" << std::fixed <<std::setprecision(2) << score5;
outputFile <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)" <<endl;
}
//close both the files
outputFile.close();
inputFile.close();
return 0;
}
================================================================================
Screen shot of the code for your reference:
Input:
Content of the input file is :
Meryl Steep 100 105 98.5 99.5 106
Bob Jones 99 106 108 101.5 102
Steve White 100 102 105 109 97.5
Output:
Content of the out put file is as below:
(Meryl Steep Test Scores: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Average Score:.......................101.80 Test score 1: ........................100.00 Test score 2: ........................105.00 Test score 3: ........................98.50 Test score 4: ........................99.50 Test score 5: ........................106.00~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)
(Bob Jones Test Scores: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Average Score:.......................103.30 Test score 1: ........................99.00 Test score 2: ........................106.00 Test score 3: ........................108.00 Test score 4: ........................101.50 Test score 5: ........................102.00~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)
(Steve White Test Scores: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Average Score:.......................102.70 Test score 1: ........................100.00 Test score 2: ........................102.00 Test score 3: ........................105.00 Test score 4: ........................109.00 Test score 5: ........................97.50~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)
Please rate answer. Thanks