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

Below are instructions to create a beginner level C++ program using functions an

ID: 3732395 • Letter: B

Question

Below are instructions to create a beginner level C++ program using functions and files. Could you pease provide the code based on the given prototypes, files, and outputs? Thank you!

Given files:

CS140Report.txt:

Mickey CS140 78 85 76 91
Minnie CS140 54 85 67 88
Donald CS145 88 82 75
Stitch CS312
Goofy CS140 86 87 92 95
Daisy CS140 68 95 74 82
Pluto CS145 74 73 85
Cinderella CS140 78 60 52 64
SnowWhite CS145 65 68 58
PrinceCharming CS145 82 89 95
Lilo CS140 53 52 71 51
Gaston CS240
Belle CS140 100 100 100 100
Beast CS140 74 73 63 77
Chip CS145 75 82 80
Simba CS145 84 89 82
Mufasa CS140 82 83 82 85
Nala CS145 95 84 90
Timon CS145 91 95 89
Scar CS140 71 81 78 88
Ariel CS140 95 86 91 88
Flounder CS145 68 52 71
Sebastian CS140 67 93 88 78

CS145Report.txt:

Happy CS145 81 85 91
Sneezy CS145 67 75 75
Doc CS145 92 97 89
Dopey CS111
Grumpy CS145 75 65 66
Bashful CS145 81 82 81
Sleepy CS145 71 74 71

const int NUM140GRADES = 4;

const int NUM145GRADES = 3;

void openFile(ifstream &in, string fileName);

void openFile(ofstream &out, string fileName);

void outputHeaders(ofstream &out, string className);

double readGradesCalcAvg(ifstream &fin, int numGrades);

char determineLetterGrade(double avg);

void printTotals(ofstream &out, double topGrade, double avg);

Write a program that reads records from a file and then depending on what is read in, writes the record to one of two output files. Below are the prototypes for the functions the program will use - do not change them. void openFile(ifstream ∈, string fileName); void openFile(ofstream &out;, string fileName); void outputHeaders (ofstream &out;, string className); This function opens the input file stream with the filename and makes sure the file opens correctly This function opens the output file stream with the filename and makes sure the file opens correctly This prints the headers to the output stream passed into it. It uses the className passed in to print that in the header next to "Class Report - ". It also prints the asterisks, and the column titles "Student Name", "Average and "Grade" double readGradesCalcAvg(ifstream &fin;, int numGrades) This function reads in the grades from the file and returns the average, numGrades is passed in - the argument is a constant depending on what class it is reading in char determineLetterGrade (double avg); This function has the average passed in and returns the letter grade (A, B, C, D, F, or X) It returns an X if the average is not between 0 and 100 void printTotals (ofstream &out;, double topGrade, double avg); This function prints the "Summary and asterisks as well as the highest grade and class average to the output stream that is passed in The program will also have 2 constants declared globally (above main) const int NUM140GRADES -4; const int NUM145GRADES 3 The main should create the output shown below. It should ask the user to type in the file name and open all the files. The output files are called "CS140Report.txt" and "CS145Report.txt". Main then outputs the headers and starts reading the data from the file. For each record, it reads in the student and class and then depending on the class it reads in the grades and calculates the average with the correct stream. It the finds the letter grade and writes the answers to the correct file. It continues doing this until all records are if the class name is not CS140 or CS145, it puts a message on the screen for the user to know. At the end, it prints out the summary Some Hints to get started: You might want to start by just reading in from the file and printing to the screen. Once you have that working, you might want to start writing to the files. 1. You are creating two output files: CS140Report.txt and CS145Report.txt

Explanation / Answer

#include<iostream>

#include<string>

#include<iomanip>

#include<cstdlib>

#include<fstream>

using namespace std;

const int NUM140GRADES = 4;

const int NUM145GRADES = 3;

void openFile(ifstream &in, string fileName){

in.open(fileName.c_str());

if(in.fail()){

cout<<"unable to open file "<<fileName<<endl;

exit(1);

}

}

void openFile(ofstream &out, string fileName){

out.open(fileName.c_str());

if(out.fail()){

cout<<"unable to write file "<<fileName<<endl;

exit(1);

}

}

void outputHeaders(ofstream &out, string className){

out<<"Class Report - "<<className<<endl;

out<<"**********************************************************"<<endl;

out<<setw(20)<<"Student Name"<<setw(10)<<"Average"<<setw(10)<<"Grade"<<endl;

}

double readGradesCalcAvg(ifstream &fin, int numGrades){

string name, className;

int marks1, marks2, marks3, marks4, i=0;

double avg = 0;

while(!fin.eof()){

fin>>name>>className;

if(className=="CS140"){

fin>>marks1>>marks2>>marks3>>marks4;

if(numGrades==4){

avg += (marks1+marks2+marks3+marks4)/4.0;

i++;

}

}

else if(className=="CS145"){

fin>>marks1>>marks2>>marks3;

if(numGrades==3){

avg += (marks1+marks2+marks3)/3.0;

i++;

}

}

}

return avg/i;

}

char determineLetterGrade(double avg){

if(avg>=90 && avg<=100)

return 'A';

else if(avg>79 && avg<90)

return 'B';

else if(avg>69 && avg<=79)

return 'C';

else if(avg>59 && avg<=69)

return 'D';

else if(avg<60 && avg>=0)

return 'F';

else

return 'X';

}

void printTotals(ofstream &out, double topGrade, double avg){

out<<"Summary"<<endl;

out<<"*****************************************************"<<endl;

out<<"Highest Grade: "<<topGrade<<endl;

out<<"Class Average: "<<avg<<endl;

}

int main(){

string filename;

ifstream in;

ofstream out1, out2;

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

cin>>filename;

openFile(in, filename);

openFile(out1, "CS140");

openFile(out2, "CS145");

string name, className;

int marks1, marks2, marks3,marks4,i=0, c1 = 0, c2 = 0;

double avg, top_grade1=0, top_grade2=0;

outputHeaders(out1, "CS140");

outputHeaders(out2, "CS145");

while(!in.eof()){

in>>name>>className;

if(className=="CS140"){

in>>marks1>>marks2>>marks3>>marks4;

avg = (marks1+marks2+marks3+marks4)/4.0;

out1<<setw(20)<<name;

if(top_grade1<avg)

top_grade1 = avg;

out1<<setw(10)<<avg<<setw(10)<<fixed<<setprecision(1)<<determineLetterGrade(avg)<<endl;

c1++;

}

else if(className=="CS145"){

in>>marks1>>marks2>>marks3;

avg = (marks1+marks2+marks3)/3.0;

out2<<setw(20)<<name;

if(top_grade2<avg)

top_grade2 = avg;

out2<<setw(10)<<avg<<setw(10)<<fixed<<setprecision(1)<<determineLetterGrade(avg)<<endl;

c2++;

}

else{

cout<<"Invalid Class: "<<className<<", "<<name<<endl;

}

i++;

}

in.clear();

in.seekg(0, ios::beg);

if(c1==0){

out1<<"No students Entered"<<endl;

}

else{

double tot_avg1 = readGradesCalcAvg(in, NUM140GRADES);

printTotals(out1, top_grade1, tot_avg1);

}

in.clear();

in.seekg(0, ios::beg);

if(c2==0){

out1<<"No students Entered"<<endl;

}

else{

double tot_avg2 = readGradesCalcAvg(in, NUM145GRADES);

printTotals(out2, top_grade2, tot_avg2);

}

}

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