working on Chapter 7, PE 6...this is what I have so far... #include Ofstream() /
ID: 3627620 • Letter: W
Question
working on Chapter 7, PE 6...this is what I have so far...#include Ofstream() //To Write Ifstream() // To Read Void calc_avg(); Void print (); Int main ( ) { While (!eof()) { Infile.gendergpa; Calc_average ( gender , gpa, &totalmale, &totalfem, &avgmale, &avgfem); } Print (&totalmale,&totalfem,&avgmale,&avgfem); Return0 } Calc_avg (_______); Void calc.avg (char gender, double gpa,double &totalfem,double &totalmal, &avgm, &avgF); If gender ==F Totalf++ Etc etc for male Etc etc for averages.
Explanation / Answer
#include <iostream> #include <fstream> using namespace std; //Variables int countMale, countFemale; float sumMaleGPA, sumFemaleGPA; float avgMaleGPA, avgFemaleGPA; //Open input and output files void openFiles (ifstream &inFile, ofstream &outFile) { //Get information from input file inFile.open ("Ch7_Ex6Data.txt"); if (inFile.fail()) { cout <<"Can not find the file input.txt"; cout <<"Exiting the program..."; system ("pause"); exit(0); } //Opens the output file outFile.open ("Ch7_Ex6Output.txt"); if (outFile.fail()) { cout <<"Can not create the output file"; system ("pause"); exit (0); } //Set precision outFile.setf(ios::fixed,ios::floatfield); outFile.precision (2); //Output to console cout.setf(ios::fixed,ios::floatfield); cout.precision(2); } void initialize()//Initializing Variables { countMale=0; countFemale=0; sumMaleGPA=0.0; sumFemaleGPA=0.0; avgMaleGPA=0.0; avgFemaleGPA=0.0; } //Sum of the student grades void sumGrades (ifstream &inFile) { char gender; double gpa; while (!inFile.eof()) { inFile>>gender; inFile>>gpa; if(gender=='M'||gender=='m') { sumMaleGPA += gpa; countMale++; } else { //Initialize Variables sumFemaleGPA; countFemale++; } } //Close input file inFile.close(); } //Find average student grades void averageGrade() { avgMaleGPA=sumMaleGPA/countMale; avgFemaleGPA=sumFemaleGPA/countFemale; } //Print void printResults(ofstream &outFile) { outFile<<"Number of Male Students: "<<countMale<<endl; cout<<"Number of Male Students: "<<countMale<<endl; outFile<<"Sum of male students GPA: "<<sumMaleGPA<<endl; cout<<"Sum of male students GPA: "<<sumMaleGPA<<endl; outFile<<"Average of male students GPA: "<<avgMaleGPA<<endl<<endl; cout<<"Average of male students GPA: "<<avgMaleGPA<<endl<<endl; outFile<<"Number of Female Students: "<<countFemale<<endl; cout<<"Number of Female Students: "<<countFemale<<endl; outFile<<"Sum of female students GPA: "<<sumFemaleGPA<<endl; cout<<"Sum of female students GPA: "<<sumFemaleGPA<<endl; outFile<<"Average of female students GPA: "<<avgFemaleGPA<<endl<<endl; cout<<"Average of female students GPA: "<<avgFemaleGPA<<endl<<endl; //Close output file outFile.close (); } int main() { //input file ifstream inFile; //output file ofstream outFile; //Calls openFiles(inFile,outFile); initialize(); sumGrades(inFile); averageGrade(); printResults(outFile); system ("pause"); return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.