I think I understand functions but maybe not. I am getting an error on my open files function. I looked around a bit and didn't find any answers. The compile dies on line 36 'char' should be preceded by ')' I don't know whats wrong. Any help would be appreciated.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std; 

    //function prototypes
void OpenFiles (ifstream& input, char lettercode, double GPA);
void Initialize (int fcount, int mcount, double f_gpa, double m_gpa);
void Sum_GPA (double& sumFemaleGPA, double& sumMaleGPA, int& fcount, int& mcount);
void Average_GPA (double& fgpa_aver, double& mgpa_aver, int& fcount, int& mcount);
void Print_Results (char lettercode, double mgpa_aver, double fgpa_aver, int fcount, int mcount);

const int eof = -999;

int main ()
{
        //Step 1
    char lettercode;      //Male or Female letter
    double GPA;	         // GPA from file
    double f_avg;         //average for female
    double m_avg;         //average for male 
    double fcount;        //female counter
    double mcount;        //male  counter
    ifstream input;       //input stream variable
    ofstream outfile;     //output stream variable 

  OpenFiles(char lettercode, double GPA); // Line 36

 Initialize(double f_gpa, double m_gpa, double& F_GPA_Average, double& M_GPA_Average);
 
    while (!eof)                   
   {
 Sum_GPA(ifstream& input, char lettercode, int& fcount, int& mcount, double& fGPA, double& mGPA);
    Average_GPA(double& fgpa_aver, double& mgpa_aver);
   } // end while 

Print_Results (ofstream outfile, char lettercode, double mgpa_aver, double fgpa_aver, int fcount, int mcount);

  input.close();
  outfile.close();

return 0;
}
    void OpenFile(char lettercode, double GPA)
    {
    ifstream input;
    input.open("ch7_ex4.txt");

        if (!input)
       {
       cout << "Unable to open file." << endl;
       cout << "Program terminates." << endl;
        return 1;
       }
     ofstream output;
     output.open ("Avr_GPA.txt");
     outfile << fixed << showpoint << setprecision(2);
    }
void Initialize (int fcount, int mcount, double f_gpa, double m_gpa)
	{
	f_gpa = 0.0;
	m_gpa = 0.0;
	fcount = 0;
	mcount = 0;
	}

void Sum_GPA(double& f_gpa, double& m_gpa, int& fcount, int& mcount)
	{
	input (lettercode, GPA);

	double fGPA;
	double mGPA;
	char lettercode;

        switch (lettercode)
       {
           case 'f':
           case 'F':
           fGPA += GPA;
           fcount ++;
         break;

           case 'm':
           case 'M':
           mGPA += GPA;
           mcount ++;
           break;
   } // end switch 
}   // end Sum_GPA function

void Average_GPA(double& fgpa_aver, double& mgpa_aver)
	{
	int fcount;
	int mcount;
	fgpa_aver = fGPA/fcount;
	mgpa_aver = mGPA/mcount;

	} //end Average_GPA function

void Print_Results (ofstream outfile, char lettercode, double mgpa_aver, double fgpa_aver, int fcount, int mcount)
	{
outfile << "Number of Female students: " << fcount << endl;
ouftile << "Female students average: " << fgpa_aver << endl;
outfile << "Number of Male students: " << mcount << endl;
outfile << "Male students average: " << mgpa_aver << endl;
	}

Whenever you call a function, you do not need to include the argument's datatypes. For instance:

// Function that returns nothing and takes 1 int as argument
void myFunction( int myInt )
{
    cout << "Your number: " << myInt << "\n";
}

int main()
{
    int myInt = 5;
    myFunction( myInt );
}

Secondly, the declaration and definition of your Print_Results are different( on line 11, the ofstream argument is missing ).

Edited 6 Years Ago by thelamb: n/a

In addition to thelamb's explanation, that means you have to remove the type name in line 33 and line 34. Also, remember to remove the '&' (address of) symbol. If you were to include this symbol in the caller function, pointers have to be declared as your function's parameter.
For more information, you can look up topics about passing parameters by pointer and passing parameters by reference.

I have updated the program and now it is having a problem with line 83 trying to get the data from the file that I opened earlier in a function error is " The "error C2064: term does not evaluate to a function taking 2 arguments" I've looked around and googled but I'm not finding anything that looks like it relates. Also where do I check to see if the file exsists? I tried in the sum_grades function but I was returning a 1 to main and it didn't like that. I've looked at this for hours any help would be appreciated. This is beginning C++ class.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <ostream>
using namespace std; 

    //function prototypes
void OpenFiles (char lettercode, double GPA);
void Initialize (int fcount, int mcount, double f_gpa, double m_gpa);
void Sum_GPA (char lettercode, double& fGPA, double& mGPA, int& fcount, int& mcount);
void Average_GPA (double& fgpa_aver, double& mgpa_aver, int& fcount, int& mcount);
void Print_Results (char lettercode, double mgpa_aver, double fgpa_aver, int fcount, int mcount);

const int eof = -999;

int main ()
{
        //Step 1
    ifstream input;
    ofstream out;
    char lettercode;      //Male or Female letter
    double GPA;	         // GPA from file
//    double f_avg;         //average for female
//    double m_avg;         //average for male 
    int fcount;        //female counter
    int mcount;        //male  counter
    double fGPA;
    double mGPA;
    double fgpa_aver;
    double mgpa_aver;

    OpenFiles(lettercode, GPA);

    Initialize(fcount, mcount, fGPA, mGPA);
/*if (!input)
{
 cout << "Unable to open file." << endl;
 cout << "Program terminates." << endl;
  return 1;
  }
*/
     while (!eof)                   
   {
      Sum_GPA(lettercode, fGPA, mGPA, fcount, mcount);

      Average_GPA(fgpa_aver, mgpa_aver, fcount, mcount);

   } // end while 

    Print_Results (lettercode, mgpa_aver, fgpa_aver, fcount, mcount);

input.close();

return 0;
}
    void OpenFiles(char lettercode, double GPA)
    {
       ifstream input;
       input.open("ch7_ex4.txt");

       ofstream output;
       output.open("GPAout");
}

    void Initialize (int fcount, int mcount, double fGPA, double mGPA)
	{
	fGPA = 0.0;
	mGPA = 0.0;
	fcount = 0;
	mcount = 0;
	}

void Sum_GPA(ifstream& input, char lettercode, double GPA, double& fgpa, double& mgpa, int& fcount, int& mcount)
{
	input(lettercode, GPA); line 83 getting the error

	double fGPA;
	double mGPA;
//char lettercode;

        switch (lettercode)
       {
         case 'f':
         case 'F':
	fGPA += GPA;
	fcount ++;
	break;

         case 'm':
         case 'M':
	mGPA += GPA;
	mcount ++;
	break;
} // end switch 
}   // end Sum_GPA function

void Average_GPA(double& fGPA, double& mGPA, int fcount, int mcount)
{
        double fgpa_aver;
        double mgpa_aver;

        fgpa_aver = fGPA/fcount;
        mgpa_aver = mGPA/mcount;

} //end Average_GPA function

void Print_Results (char lettercode, double mgpa_aver, double fgpa_aver,int fcount, int mcount)
	{
	ofstream output;
	cout << "Number of Female students: " << fcount << endl;
	cout << "Female students average: " << fgpa_aver << endl;
	cout << "Number of Male students: " << mcount << endl;
	cout << "Male students average: " << mgpa_aver << endl;
	}

Ask yourself questions (before asking google). And be precise!
You did not read my last post fully, as the error I mentioned is still in. Don't just do something, hit compile and google for the errors you are getting, think structurally what you want to achieve, break the problem up in pieces and step by step take them down, making changes precisely and everywhere where you need to (declaration and definition should match).

After fixing what I mentioned, think about what input is.

Edited 6 Years Ago by thelamb: n/a

Honelstly I really am lost and don't fully understand your answer.
This is how I understand it, your void prototypes before 'main' let the compiler know that there are user defined functions in the program and they are not returning any value
The actual variables in the prototype are what is going to be used in the function. The call to the function lists the same variables w/o the datatypes, these are what is going to be used in the function. You use reference passing when there is a variable that will change.
In the OpenFiles function prototype there are two data items the lettercode(either m or f) and the GPA. In the Openfiles function I have the same vairables as the prototype. How far off am I?
I've added the 'ofstream output' to the Print_Detail prototype. The error is that the call Print_Detail doesn't take 5 arguments. To me that says I have to many listed in the call I tried adding the ofstream to the call and that didn't work.
I know its probably something real simple to you but I'm not a programmer and C++ is new to me. I have a better understanding of COBOL if tells you anything. Thank you for trying to help me understand this.

I've added the 'ofstream output' to the Print_Detail prototype. The error is that the call Print_Detail doesn't take 5 arguments.

input(lettercode, GPA); line 83 getting the error

No. The error is that input is not a function of 2 arguments. In fact, it is not a function at all. According to the Sum_GPA signature, it is a reference to ifstream . If you want to read something from it, you need to call its methods, e.g.

input >> lettercode;
    input >> GPA;

Before you do that, however, be advised, that the OpenFiles function is meaningless as written. The input and output mentioned there have nothing in common with other variables of the same name.

when you say "have nothing in common with other variables of the same name. " Are you talking about the lettercode and GPA? Am I opening the files incorrectly? Don't you have to use the same variables in the function? the lettercode and GPA never change they just get read from the file? This is horrible I feel so stupid right now.

I have made changes and now I am getting one error on line 49 error C2660: 'Print_Results' : function does not take 4 arguments.
To me the error is saying that I either have too many arguments or not enough.
I have taken the ofstream out of the prototype and it gave me errors that my variables were unreferenced. I put the the ofstream in the the function body. I put the ofstream in the function parm list. nothing works. Please someone let me know if I do not understand this correctly. In the prototpe I have to let the compiler know that the arguments are in the ofstream file thats why I include the ofstream output.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <ostream>
using namespace std; 

    //function prototypes
void OpenFiles (char& lettercode, double& GPA);
void Initialize (int fcount, int mcount, double f_gpa, double m_gpa);
void Sum_GPA (char lettercode, double GPA, double& fGPA, double& mGPA, int& fcount, int& mcount);
void Average_GPA (double& fgpa_aver, double& mgpa_aver, int& fcount, int& mcount);
void Print_Results (ofstream& output, double& mgpa_aver, double& fgpa_aver, int& fcount, int& mcount);
const int eof = -999;

int main ()
{
        //Step 1
    ifstream input;
    ofstream output;
	char lettercode;     //Male or Female letter
    double GPA;	         // GPA from file
    int fcount;          //female counter
    int mcount;          //male  counter
	double fGPA;
	double mGPA;
	double fgpa_aver;
	double mgpa_aver;

	OpenFiles(lettercode, GPA);

    Initialize(fcount, mcount, fGPA, mGPA);
	
     while (!eof)                   
   {
     Sum_GPA(lettercode, GPA, fGPA, mGPA, fcount, mcount);
	 
     Average_GPA(fgpa_aver, mgpa_aver, fcount, mcount);

   } // end while 
	Print_Results (mgpa_aver, fgpa_aver, fcount, mcount);

	input.close();

return 0;
}
	
	void OpenFiles(char& lettercode, double& GPA)
    {		
		ifstream input;
		input.open("ch7_ex4.txt");
	

		if (!input)
		 {
		  cout << "Unable to open file." << endl;
		  cout << "Program terminates." << endl;
	     }
//	    input >> lettercode;
//		input >> GPA;

	    ofstream output;
		output.open("GPAout");
	}

    void Initialize (int fcount, int mcount, double fGPA, double mGPA)
	{
		fGPA = 0.0;
		mGPA = 0.0;
		fcount = 0;
		mcount = 0;
	}

	void Sum_GPA(char lettercode, double GPA, double& fgpa, double& mgpa, int& fcount, int& mcount)

	{	
		ifstream input;
		double fGPA;
		double mGPA;
		
		input >> lettercode;
		input >> GPA;

	    switch (lettercode)

		{
			case 'f':
			case 'F':
				fGPA += GPA;
				fcount ++;
			break;

			case 'm':
			case 'M':
				mGPA += GPA;
				mcount ++;
			break;
		} // end switch 
	}   // end Sum_GPA function

	void Average_GPA(double& fGPA, double& mGPA, int& fcount, int& mcount)
	{
	double fgpa_aver;
	double mgpa_aver;

	fgpa_aver = fGPA/fcount;
	mgpa_aver = mGPA/mcount;

	} //end Average_GPA function
	
   void Print_Results (double& mgpa_aver, double& fgpa_aver, int& fcount, int& mcount)
	{
ofstream output;
output << "Number of Female students: " << fcount << endl;
output << "Female students average: " << fgpa_aver << endl;
output << "Number of Male students: " << mcount << endl;
output << "Male students average: " << mgpa_aver << endl;

	output.close();
	
	}

when you say "have nothing in common with other variables of the same name. " Are you talking about the lettercode and GPA? Am I opening the files incorrectly? Don't you have to use the same variables in the function? the lettercode and GPA never change they just get read from the file? This is horrible I feel so stupid right now.

Removing all irrelevant lines, the OpenFiles function boils down to the following:

void OpenFiles(char& lettercode, double& GPA)
{		
	ifstream input;
	input.open("ch7_ex4.txt");

	ofstream output;
	output.open("GPAout");
}

It declares two local variables input and output , does some magic with them and returns. One very important thing you must realize is that once the function returns, its local variables are gone, as if they never existed. As coded above, the function has the same effect at the rest of the program as if it was

void OpenFiles(char& lettercode, double& GPA)
{		
	ifstream foo;
	foo.open("ch7_ex4.txt");

	ofstream bar;
	bar.open("GPAout");
}

Later on, you try to use input in

void Sum_GPA(char lettercode, double GPA, double& fgpa, double& mgpa, int& fcount, int& mcount)
{	
	ifstream input;
	input >> lettercode;
	input >> GPA;

and again, input is a local variable. It does not relate in any sense to input which was declared in OpenFiles .

Now I know why the instructor said functions were a difficult concept to get your mind around. I'm going to start drinking again.

I understood that when you use a pass by reference variable that is how you pass values outside of the function to be used somewhere else. From the book "reference parm can pass one or more values from a function and can change the value of the actual parm". And your post is saying that input and output are local variables. I'm not getting the conection btwn the prototype, function call stmt and the function. I thought the prototype laid the ground work telling the compiler there is a user defined function and it has two variables one char reference & one reference double. In the function call its supposed to use the variables designated in the prototype. If as you are telling me that input and output are local variables how am I supposed to get them passed so they can be used in the other function? I thought the the function uses substitution for the variable names. or am I supposed to do the read from the file in OpenFiles and pass it that way?

Finally figured out how to do this pgm. Thank you all for your help, I really appreciate the feedback.

This question has already been answered. Start a new discussion instead.