Ok, This program is complete, but it will not actually do the calculations and s
ID: 3627413 • Letter: O
Question
Ok, This program is complete, but it will not actually do the calculations and store it to the output file. It says that is not creating the output file,but it is. It runs to the line of <<"Can not create the output file". Can you give any suggestions to where it will acutally calculate and put to output file. It should not be that difficult for an experienced programmer. I don't think!#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;
}
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;
}
Explanation / Answer
find this line
if (outFile.fail());
delete the ;
if (outFile.fail())
works for me then.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.