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

Here is what I have come up with so far. The program compiles but it keeps sayin

ID: 3622123 • Letter: H

Question

Here is what I have come up with so far. The program compiles but it keeps saying that it can't open the input file. I checked and made sure that I put the proper txt file into the program but it will not read the input file no matter what I do. What is wrong here?

#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;

int openfiles(ifstream& GPAfile, ofstream& outfile);
void initialize(float& sumFemale, float& sumMale, int& countFemale, int& countMale, int& code);
void sumGrades(float gpa, float& sum, int& count);
void averageGrades(float& avgFemale, float& avgMale, float sumFemale, float sumMale, int countFemale, int countMale);
void writeFile(ofstream& outfile, float avgFemale, float avgMale);



int main()
{
float sumFemale, sumMale;
float avgFemale, avgMale;
int countFemale, countMale;
int code;
char gender;
float gpa;
ifstream GPAfile;
ofstream outfile;


code=openfiles(GPAfile,outfile);

if (code = -1)
{
return 0;
}

initialize(sumFemale,sumMale,countFemale,countMale,code);

GPAfile>>gender>>gpa;
while (gpa != -999)
{
if (gender = 'f')

sumGrades(gpa, sumFemale, countFemale);

else if (gender = 'm')

sumGrades(gpa, sumMale, countMale);
GPAfile >> gender >>gpa;

}

averageGrades(avgFemale, avgMale, sumFemale, sumMale, countFemale, countMale);
writeFile(outfile,avgFemale,avgMale);



GPAfile.close();
outfile.close();

return 0;
}

int openfiles(ifstream& GPAfile, ofstream& outfile)
{
GPAfile.open("gpaData.txt");
if (GPAfile.fail())
{
cout<<"Input file did not open!"<
cout<<"Program will terminate!";
return 1;
}
outfile.open("GPAaverages.txt");
if (outfile.fail())
{
cout<<"Output file did not open!";
cout<<"Program will terminate!";
return 1;
}
outfile <<< showpoint;
outfile << setprecision (2);
return 0;
}


void initialize(float& sumFemale, float& sumMale, int& countFemale, int& countMale, int& code)
{
sumFemale = 0;
sumMale = 0;
countFemale = 0;
countMale = 0;
code = 0;
}

void sumGrades(float gpa, float& sum, int& count)
{
sum = sum + gpa;
count = count + 1;
}

void averageGrades(float& avgFemale, float& avgMale, float sumFemale, float sumMale, int countFemale, int countMale)
{
avgFemale = sumFemale / (float) countFemale;
avgMale = sumMale / (float) countMale;

}

void writeFile(ofstream& outfile, float avgFemale, float avgMale)
{
outfile<<"Average for females is:"<<
outfile<<"Average for males is: "<<
}





Explanation / Answer

Hope this helps. Let me know if you have any questions. Please rate. :) Did the program actually output that the file couldn't open, or did it just exit the program? Regardless of whether or not the file opened, it would exit as you have it there, for two reasons. First, the line that checks the return value of the file open function is comparing it to -1, but your open function returns 1 for fail, not -1, so that should be changed to 1. Second, you aren't actually comparing it to 1, it's an assignment, which should be changed to a comparison using ==. You have "if (code = -1)" and it should be "if (code == 1)". You do this a few other places also, like "if (gender = 'f')" should be "if (gender == 'f')". I've fixed those things I named. If this still doesn't work for you, feel free to comment or send me a private message, and I'd be happy to help more before you rate. (Or if you're happy with this, feel free to go ahead and rate. Thanks!) Here ya go: #include #include #include using namespace std; int openfiles(ifstream& GPAfile, ofstream& outfile); void initialize(float& sumFemale, float& sumMale, int& countFemale, int& countMale, int& code); void sumGrades(float gpa, float& sum, int& count); void averageGrades(float& avgFemale, float& avgMale, float sumFemale, float sumMale, int countFemale, int countMale); void writeFile(ofstream& outfile, float avgFemale, float avgMale); int main() { float sumFemale, sumMale; float avgFemale, avgMale; int countFemale, countMale; int code; char gender; float gpa; ifstream GPAfile; ofstream outfile; code=openfiles(GPAfile,outfile); if (code == 1) { return 0; } initialize(sumFemale,sumMale,countFemale,countMale,code); GPAfile>>gender>>gpa; while (gpa != -999) { if (gender == 'f') sumGrades(gpa, sumFemale, countFemale); else if (gender == 'm') sumGrades(gpa, sumMale, countMale); GPAfile >> gender >>gpa; } averageGrades(avgFemale, avgMale, sumFemale, sumMale, countFemale, countMale); writeFile(outfile,avgFemale,avgMale); GPAfile.close(); outfile.close(); return 0; } int openfiles(ifstream& GPAfile, ofstream& outfile) { GPAfile.open("gpaData.txt"); if (GPAfile.fail()) { cout