Thread: user define function trouble!

  1. #1
    johnnypiere
    Guest

    user define function trouble!

    i write this program in an excercise.. as a main funtion.. then rewrite it using function call by user define functions. I get lost. Please point out what i am wrong, and be gentle with me cause i am a beginner.. /

    code for the first program.
    -------------------------------------
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    void main()
    {	ofstream out;
    	ifstream prob;
    	char ch;
    	static float fgpa = 0.0, mgpa= 0.0;
    	float gpa, avfgpa, avmgpa;
    	int fcout = 0, mcout = 0;
    	prob.open("d:\\prob7-4.txt", ios::in);
    	if (!prob)
    	{
    		cout<<"Can not open input file"<<endl;
    		cout<<"program terminates!!"<<endl;
    		return;
    	}
    	out.open("d:\\out.txt", ios::out);
    	out<<fixed<<showpoint;
    	out<<setprecision(2);
    
    	prob.get(ch);
    	prob>>gpa;
    	while(!prob.eof())
    	{
    		switch (ch)
    		{
    		case 'F':
    		case 'f': fgpa = fgpa+gpa;
    			fcout++;
    			avfgpa = fgpa/fcout;
    			break;
    		case 'M':
    		case 'm': mgpa = mgpa + gpa;
    			mcout++;
    			avmgpa = mgpa/mcout;
    			break;
    		default: cout<<"invalid gender"<<endl;
    			return;
    		}
    		prob>>ch>>gpa;
    
    	}
    	
    	out<<"Number of female ="<<fcout<<endl;
    	out<<"Average female GPA  = "<<avfgpa<<endl;
    	out<<"Number of male ="<<mcout<<endl;
    	out<<"Average male GPA ="<<avmgpa<<endl;
    	out.close();
    	return;
    }
    -----------------------------------
    code for the function call rewrite from above

    ------------------------------------
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    void OpenFiles(char ch, float gpa);
    void initialize(int fcout, int mcout, float fgpa, float mgpt);
    void sumGrades( int fcout, int mcout, float fgpa, float mgpa);
    void averageGrades(float avfgpa, float avmgpa);
    void printResults(int fcout, int mcout, float avfgpa, float avmgpa);
    void main()
    
    {
    	ofstream out;
    	ifstream in;
    	char ch;
    	float gpa;
    	float avfgpa;
    	float avmgpa;
    	int fcout, mcout;
    	float fgpa;  //define female GPA
    	float mgpa;  //define male GPA
    
    	initialize (fcout, mcout, fgpa, mgpa);
    
    	OpenFiles(ch, gpa);
    
    	while(!in.eof())
    	{
    		sumGrades(fgpa,mgpa,fcout,mcout);
    		in>>ch>>gpa;
    		averageGrades(avfgpa, avmgpa);
    
    	}
    
    
    	printResults(fcout, mcout, avfgpa, avmgpa);
    
    	
    	return;
    	
    }
    
    void OpenFiles(char ch, float gpa)
    {
    
    	ofstream out;
    	ifstream in;
    	in.open("k:\\prob-7.txt",ios::in);
    	if (!in)
    	{
    		cout<<"Can not open input file"<<endl;
    		cout<<"program terminates!!"<<endl;
    	}
    	in.get(ch);
    	in>>gpa;
    	in.eof();
    
    	out.open("k:\\out.txt", ios::out);
    	out<<fixed<<showpoint;
    	out<<setprecision(2);
    	//out<<"Female" <<ch<<endl;
    
    }
    
    void initialize(int fcout, int mcout, float fgpa, float mgpa)
    
    {
    
    	fgpa = 0.0;
    	mgpa= 0.0;
    	fcout = 0;
    	mcout = 0;
    }
    
    void sumGrades(int fcout, int mcout, float fgpa, float mgpa)
    {
    	
    	char ch;
    	float gpa;
    	OpenFiles(ch, gpa);
    	switch (ch)
    	{
    		case 'F':
    		case 'f': fgpa = fgpa+gpa;
    			fcout++;
    			//avfgpa = fgpa/fcout;
    			break;
    		case 'M':
    		case 'm': mgpa = mgpa + gpa;
    			mcout++;
    			//avmgpa = mgpa/mcout;
    			break;
    		default: cout<<"invalid gender"<<endl;
    			return;
    	}
    	
    }
    
    void averageGrades(float avfgpa, float avmgpa)
    {
     	
    	 float fgpa,  mgpa;
    	 int fcout, mcout;
    	 sumGrades(fcout, mcout, fgpa, mgpa);
    	 avfgpa = fgpa/fcout;
    	 avmgpa = mgpa/mcout;
    }
    	
    void printResults(int fcout, int mcout, float avfgpa, float avmgpa)
    {
    	ofstream out;
    	out<<"Number of female ="<<fcout<<endl;
    	out<<"Average female GPA  = "<<avfgpa<<endl;
    	out<<"Number of male ="<<mcout<<endl;
    	out<<"Average male GPA ="<<avmgpa<<endl;
    	out.close();
    }

  2. #2
    Code tags were added to this post so that you have a snowballs chance in hell of someone actually reading all that.

    Use [ code ] (to open) and [ /code ] (to close) (minus the spaces) to format code.

    Since theres a lot of code there and I dont feel like reading it all, could you be a little more specific about whats wrong with it so I know where to look? Does it not compile? What errors?
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    hi

    i am johnnypiere

    ...

    the first program write in main alone is working fine.

    the second one does not compile, and give a lots of error vs warning, I think that having trouble with function calls, and passing to main function, but i have no idea to fix cause the book i am studying hasnt clear out much.

    summerize what program will do :

    open a file with 2 columes; one is letter of f or m (represent for female and male), the other colume is GPA (grade).

    read it and sum the gpa of female , sum the gpa of male, and display the everage of female and male gpa.

    the function calls need to have 5 of them:

    initialize variable

    open and format output

    sum gpa of female and male

    find the average of female male gpa

    print the rerults.
    Last edited by john111111; 10-16-2002 at 12:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM