Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Output example: scription write a program that reads a student name and test gra

ID: 3596822 • Letter: O

Question


Output example:

scription write a program that reads a student name and test grades from a file. The students are allowed to retake the test up to ten times, and the average of the test scores is the score they receive on the test. Each record of the file is stored in the following manner Mickey 5 95 85 72 68 59 Spaces separate the fields in the file. The first field is the students name (it will always be a one word string). The second field (in this case the 5) tells how many times they took the test. The remaining fields are the test scores The output will also go to a file Your program should have the following functions openFile This function opens and tests the input file. The input file stream and the file openFile This function opens and tests the output file. The output file stream and the calculateAverage- this function has the input stream and the number of attempts passed name for the input file are passed into the function. file name for the output file are passed into the function. into it. It finds the average of the numbers and returns the average Requirements/Hints/ stions 1. Comments at the top of the program 2. Main calls all functions it asks the user to type in the input and output file names it calls the functions to open the files - it reads the name and number of attempts it calls average to compute the average of the test scores it writes the information to the output file it continues doing this until all of the records have been processed . Submit the cpp file to moodle by the due date

Explanation / Answer

#include<iostream>

#include<fstream>

#include<string>

#include<iomanip>

using namespace std;

//function declarations

int openFile(ifstream &in, string inFile);

int openFile(ofstream &out, string outFile);

float calculateAverage(ifstream &in, int no_attempts);

int main()

{

string inFile, outFile;

ifstream in;

ofstream out;

int no_attempts;

string name;

cout << "Enter the input file name: ";

cin >> inFile;

cout << "Enter the output file name: ";

cin >> outFile;

//open input file

if (openFile(in, inFile)< 0 )

{

return -1;

}

//open output file

if (!openFile(out, outFile) < 0 )

{

return -1;

}

float avg;

out << "Name " << "# Attempts " << "Average" << endl;

while (!in.eof())

{

in >> name >> no_attempts;

avg = calculateAverage(in, no_attempts);

//write to output

out << std::fixed;

out << std::setprecision(1);

out << left << name << " " << right << no_attempts << " " << right << avg << endl;

}

in.close();

out.close();

}

int openFile(ifstream &in, string inFile)

{

//open input file by name inFile

in.open(inFile);

//chk if it's open

if (!in)

{

cout << "Not able to open input file" << endl;

return -1;

}

return 0;

}

int openFile(ofstream &out, string outFile)

{

//open output file by name outFile

out.open(outFile);

//chk if it's open

if (!out)

{

cout << "Not able to open output file" << endl;

return -1;

}

return 0;

}

float calculateAverage(ifstream &in, int no_attempts)

{

int sum = 0,m;

while (in.peek() != ' ' )

{

if (in.peek() == EOF)

break;

in >> m;

sum += m;

}

return (float)sum / no_attempts;

}

/*output:

Name # Attempts Average

Mickey 5 75.8

Minnie 2 96.0

Goofy 1 98.0

Pluto 3 87.7

Donald 10 64.5

Daisy 6 72.0

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote