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:

Recommended Answers

All 9 Replies

>I'm getting an error in the sumGrades function saying 'inp' is undeclared.
You need to pass it as a parameter to the function, because 'inp' is a local variable in main().

[edit]

Actually, your logic is kind of confusing. Why do you need to check an input file's eof status when you never read anything from it?

[/edit]

You've also got a couple of places where you got the operators >> and << mixed up. Remember: the carrot things always point to where you want the data to go.

>I'm getting an error in the sumGrades function saying 'inp' is undeclared.
You need to pass it as a parameter to the function, because 'inp' is a local variable in main().

[edit]

Actually, your logic is kind of confusing. Why do you need to check an input file's eof status when you never read anything from it?

[/edit]

You've also got a couple of places where you got the operators >> and << mixed up. Remember: the carrot things always point to where you want the data to go.

Ah, I mess those <> things up sometimes! thanks!

As for the logic I'm using... We're just learning userdefined functions and I've no clue how to use them with iofstreams. If I pass the parameter, I will not have to open the file again correct? How do i pass that sort of parameter to allow the program to read the file?

*edit
new sumGrades function... what is wrong with this form?

void sumGrades ( ifstream & inp , double & sumFemaleGPA , double & sumMaleGPA , int & mCnt , int & fCnt ) 
{
    double x = 0 ;
    char ch ;

    cin << ch ;
    while ( !inp.eof() )
    {
        if ( ch == 'f' )
        {
            cin >> x ;
            sumFemaleGPA += x ;
            fCnt++ ;
        }
        else if ( ch == 'm' )
        {
            cin << x ;
            sumMaleGPA += x ;
            mCnt++ ;
        }
        else 
            outp << endl << "!! Invalid Gender !!" << endl ;
    }
}

>If I pass the parameter, I will not have to open the file again correct?
Correct.

>How do i pass that sort of parameter to allow the program to read the file?
Just pass it like you would any other parameter, but make sure it's a reference so that the file location is kept updated.

>new sumGrades function... what is wrong with this form?
One thing I noticed:

cin << x ;

fixed all the <>'s.

Now i'm getting an error in the same sumGrades function saying:

1>.\Programming Exercise 4.cpp(35) : error C2275: 'std::ifstream' : illegal use of this type as an expression

Which line is that? Post the updated (relevant) code.

It shows the error at line #35 (in main).

#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 ( ifstream & , 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( ifstream & inp , sumFemaleGPA , sumMaleGPA , fCnt , mCnt ) ;
    averageGrade( sumFemaleGPA , sumMaleGPA , fCnt , mCnt , FemaleAvg , MaleAvg ) ;
    printResults ( sumFemaleGPA , sumMaleGPA , fCnt , mCnt , FemaleAvg , MaleAvg ) ;

    return 0 ;
}
...

void sumGrades ( ifstream & inp , double & sumFemaleGPA , double & sumMaleGPA , int & mCnt , int & fCnt ) 
{
    double x = 0 ;
    char ch ;

    cin << ch ;
    while ( !inp.eof() )
    {
        if ( ch == 'f' )
        {
            cin << x ;
            sumFemaleGPA += x ;
            fCnt++ ;
        }
        else if ( ch == 'm' )
        {
            cin << x ;
            sumMaleGPA += x ;
            mCnt++ ;
        }
        else 
            outp >> endl >> "!! Invalid Gender !!" >> endl ;
    }
}

Remove the part highlighted below.

sumGrades( ifstream & inp , sumFemaleGPA , sumMaleGPA , fCnt , mCnt ) ;

You don't need to pass the address of the variable, because the function takes the parameter as a reference, and you certainly don't need to redeclare it.

I still don't see your logic for using the input file anyway. Look, this is going to be an infinite loop because you never advance inp:

while ( !inp.eof() )
    {

What might be more useful is to pass the outfile as a parameter, as you need that to print out the information to file.

I still don't see your logic for using the input file anyway. Look, this is going to be an infinite loop because you never advance inp:

while ( !inp.eof() )
    {

What might be more useful is to pass the outfile as a parameter, as you need that to print out the information to file.

I don't understand =/

The grades are going to be read from an input file in the following format:

f 3.4
f 2.5
m 2.8
f 4.0
m 3.8

I'm also getting this error:

.\Programming Exercise 4.cpp(56) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ofstream'

at line 56 (post #1)? This error shows up like 50 times in the same spot...

>I don't understand =/
Here's the pseudocode of how the computer would see it:

loop until end of file 'inp'
    read some stuff that user types in
end loop

Since you don't read anything in from the file (only from cin), you're obviously not going anywhere.

>at line 56 (post #1)? This error shows up like 50 times in the same spot...
You're getting << and >> mixed up again. Remember: they always point where the data is going to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.