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();
}