Hey everyone,
I'm working on this program for class but I'm getting an error in the [I]sumGrades[/I] function saying 'inp' is undeclared. Could someone give me a hand?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
// initializes count of females, count of males, female GPA sum, and male GPA sum
void initialize(int & fCnt, int & mCnt, double & sumFemaleGPA, double & sumMaleGPA) ;
// opens input & output files; sets output to fixed-showpoint-precision2
void openFiles(ifstream & inF, string iNam, ofstream & outF, string oNam) ;
// adds male and female grades; produces sum of each
void sumGrades ( double & , double & , int & , int & ) ;
// finds male and female averages
void averageGrade ( double & sumFemale , double & sumMale , int & mCnt , int & fCnt , double FemaleAvg , double MaleAvg ) ;
// prints relative results
void printResults ( double & sumFemale , double & sumMale , int & mCnt , int & fCnt , double FemaleAvg , double MaleAvg ) ;
int main()
{
int fCnt ;
int mCnt ;
double sumFemaleGPA ;
double sumMaleGPA ;
double MaleAvg ;
double FemaleAvg ;
string inName = "inputTxt.txt";
string outName = "no07out.txt";
ifstream inp; // input file stream variable
ofstream outp; // output file stream variable
initialize( fCnt , mCnt , sumFemaleGPA , sumMaleGPA ) ;
openFiles( inp, inName, outp, outName ) ;
sumGrades( sumFemaleGPA , sumMaleGPA , fCnt , mCnt ) ;
averageGrade( sumFemaleGPA , sumMaleGPA , fCnt , mCnt , FemaleAvg , MaleAvg ) ;
printResults ( sumFemaleGPA , sumMaleGPA , fCnt , mCnt , FemaleAvg , MaleAvg ) ;
return 0 ;
}
void openFiles( ifstream & inF, string iNam, ofstream & outF, string oNam )
{
inF.open(iNam.c_str()); // open input data file
if ( inF.fail() ) {
cout << "Unable to open file " << iNam << endl;
return; // end the program
} // endif
outF.open(oNam.c_str()); // open output file
if ( outF.fail() ) {
cout << "Unable to open file " << oNam << endl;
return; // end the program
} // endif
outF << fixed << showpoint << setprecision(2) ;
}
void initialize ( double & sumFemaleGPA , double & sumMaleGPA , int & mCnt , int & fCnt )
{ // set count of words, words in a line, and lines to zero
fCnt = mCnt = sumFemaleGPA = sumMaleGPA = 0 ;
}
void sumGrades ( double & sumFemaleGPA , double & sumMaleGPA , int & mCnt , int & fCnt )
{
double x = 0 ;
char ch ;
while ( !inp.eof() )
{
cin << ch ;
if ( ch == 'f' )
{
cin >> x ;
sumFemaleGPA += x ;
fCnt++ ;
}
else if ( ch == 'm' )
{
cin << x ;
sumMaleGPA += x ;
mCnt++ ;
}
else
outp << endl << "!! Invalid Gender !!" << endl ;
}
}
void averageGrade ( double & sumFemale , double & sumMale , int & mCnt , int & fCnt , double FemaleAvg , double MaleAvg )
{
MaleAvg = sumMale / mCnt ;
FemaleAvg = sumFemale / fCnt ;
}
void printResults ( sumFemaleGPA , sumMaleGPA , fCnt , mCnt , FemaleAvg , MaleAvg )
{
outp << "sumFemaleGPA = " << sumFemaleGPA << endl ;
outp << "sumMaleGPA = " << sumMaleGPA << endl ;
outp << "fCnt = " << fCnt << endl ;
outp << "mCnt = " << mCnt << endl ;
outp << "FemaleAvg = " << FemaleAvg << endl ;
outp << "MaleAvg = " << MaleAvg << endl ;
}
Thanks guys! :mrgreen: