I cannot seem to spot the error here, other articles are a bit fuzzy about the answers to the error so here is mine. I am getting this error, I believe it has something to do with the files it is trying to open. I posted the entire .cpp file as I am not sure where the error stems from.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int openFiles(ifstream inFile, ofstream outFile)
{
inFile.open("finalin.dat");
outFile.open("finalout.dat");
outFile << fixed << showpoint << setprecision(2);
inFile >> fixed >> showpoint >> setprecision(2);
if (!inFile||!outFile)
{
cout << "Problem opening file.";
}
}
void initialize(int countFemale,int countMale,float sumFemaleGPA,float sumMaleGPA)
{
countFemale=0;
countMale=0;
sumFemaleGPA=0;
sumMaleGPA=0;
}
int sumGrades(ifstream inFile, float sumFemaleGPA, float sumMaleGPA,int m,int f)
{
if (!inFile)
{
inFile.open("finalin.dat");
}
char sex;
float grade;
while(!inFile.eof())
{
inFile >> sex >> grade;
switch (sex)
{
case 'f': (sumFemaleGPA + grade);
f++;
break;
case 'm': (sumMaleGPA + grade);
m++;
break;
}
}
}
int averageGPA(float avgfGPA, float avgmGPA, int m, int f, float sumFemaleGPA, float sumMaleGPA)
{
avgfGPA=sumFemaleGPA/f;
avgmGPA=sumMaleGPA/m;
}
int printResults(float avgfGPA, float avgmGPA, ofstream outFile)
{
cout <<"The average GPA of the female students is: "<< avgfGPA << endl;
cout <<"The average GPA of the male students is: "<< avgmGPA;
outFile << "The average GPA of the female students is: "<< avgfGPA << endl;
outFile <<"The average GPA of the male students is: "<< avgmGPA;
}
int main()
{
int countFemale;
int countMale;
float sumFemaleGPA;
float sumMaleGPA;
float avgfGPA;
float avgmGPA;
ifstream inFile;
ofstream outFile;
openFiles(inFile,outFile);
initialize(countFemale,countMale,sumFemaleGPA,sumMaleGPA);
sumGrades(inFile,sumFemaleGPA,sumMaleGPA,countMale,countFemale);
averageGPA(avgfGPA,avgmGPA,countMale,countFemale,sumFemaleGPA,sumMaleGPA);
printResults(avgfGPA,avgmGPA, outFile);
}
Also, I realize that having 5 functions like that is a bit messy, but that is how our professor requested it since we are to demonstrate our knowledge of functions as well.
The problem is that you cannot pass streams by value, you have to pass them by reference or pointer. in every function arguments definition add
&after streams like:instead of
Edit:
Your initialize doesn’t do anything because it takes arguments by value. you need to take them by reference to be able to modify the source, use
instead of
and the warning should go away.