Make a program so that the function main is merely a collection of function calls. Your program should use the following functions.
a. Function openFiles: This function opens the input and out files, and sets the output of the floating point numbers to two decimal places in a fixed decimal format with a decimal point and trailing zeros.
b. Function initialize: This function initializes variables such as countFemale, countMale, sumFemaleGPA, and sumMaleGPA.
c. Function sumGrades: This function finds the sum of female and male students GPA
d. Function averageGrade: This function finds the average GPA for female and male students
e. Function printResults: This function outputs the relevant results.
f. There can be no global variables. Use appropriate parameters to pass information in and out of functions

For research purposes and to better help students, the admissions office of your local university want to know how well female and male students perform in certain courses. You receive a file that contains female and male students GPA for certain courses. Due to confidentiality the letter code f is used for female students and m for male students. Every file entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries in the file is unknown. Write a program that computes and outputs the average GPA for both female and male students. Format your results to two decimal places.

heres what i have come up so far

#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>

void main()
{
ifstream inData;
char gender;
float average_gpa, sum_gpa = 0, gpa;
int count = 0;

inData.open("GPA_Detail.dat");
if(!inData){
cout<<"Cannot open input file. Program terminates!!!\n";
}

cout<<"Processing data...\n";
inData>>gender>>gpa;
count++;
while(!inData.eof()){
sum_gpa += gpa;
inData>>gender>>gpa;
count++;
}

average_gpa = sum_gpa / count;
cout<<fixed;
cout<<"The average gpa is"<<setprecision(2)<<average_gpa<<".\n";

inData.close();
}

i dont know where to put the functions in my program any tips or hint?

You're off to a good start. Look at the assigned functions, create them. In general, you would move chunks of the code you have in main( ) to the appropriate function.

In main( ), declare the variables that will be needed to store and communicate the raw data - you have most of those now. You will need to create separate count and sum variables for men and for women. These get passed to the functions, as needed.

You will pass the input and output filestream variables to the open function - remember those must be passed by reference.

Also, bring your code up to current standards and style- use the correct headers.

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

int main()

Also, please use the code tags when you post here, like:

[code]

//some c++ code goes here

[/code]

heres my progress

#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);
int 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);
	
}

void OpenFiles(char ch, float gpa)
{

	ofstream out;
	ifstream in;
	in.open("GPA_Detail.dat",ios::out | ios::ate);
	if (!in)
	{
		cout<<"Can not open input file"<<endl;
		cout<<"program terminates!!"<<endl;
	}
	in.get(ch);
	in>>gpa;
	in.eof();

	out.open("GPA_Detail.dat",ios::out | ios::ate);
	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();
}

theres no more errors when you compile it the only problem is OpenFiles(ch, gpa) and n.open("GPA_Detail.dat",ios::out | ios::ate) in the GPA_Detail di you need to make a notepad with the GPA formula in it and save it as GPA_Detail.dat? because when i run the program the program only say invalid gender im sorry if this is a noob question because im kinda new at functions

Like vmanes said, please use code tags. It makes things much easier to read.

[code]

// paste code here

[/code]

i already iedited it sorry for not listening

commented: Not often we get a sorry :) +3
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);

Think about the job the functions have to do. OpenFiles must open and check the files, but should not do any reading. When it's succeeded, main( ) must have access to the opened files. Thus, the ifstream and ofstream should be declared in main( ), passed to OpenFiles( ), and some value sent back letting main( ) know if the action succeeded or not.

(optional) In the interest of generality and easy reuse, the filename(s) should be passed to OpenFiles() as parameters, rather than being hardcoded in the function.

What purpose does the char ch and the float gpa serve in OpenFiles( )?

You're on the right track with initialize( ), but how does its action affect the values in main( )? Read up on the difference between "pass by value" and "pass by reference".

As I read the assignment, I'd use sumGrades( ) to do all the reading from the now opened file, and when it's completed processing the whole file, it gives the results back to main( ). Again, how is that information communicated back? (see previous comment.)

averageGrades( ) needs to be given the information that sumGrades( ) generated - you've got it calling the sumGrades( ) internally -why? In this problem, I don't see it structured such that any of your functions call any of the others - they just run in sequence.

printResults( ) looks good, once the data being passed to it has been properly calculated.

commented: Good post +6

so want do i need to type in.open("GPA_Detail.dat",ios::out | ios::ate) or in the GRA_detail.dat file? thats the only problem i have now

The assignment does not say you must write the results to a file, only that you must output (display) the results. I would take that as screen display.

in.open("GPA_Detail.dat",ios::out | ios::ate);

is a nonsensical statement. An ifstream by definition is for input, so ios::out is wrong. You normally want reading of an input filestream to start at the beginning, so ios:ate (AT End) is inappropriate. A correct line to open the input file would be:

in.open( "GPA_Detail.dat"" );

That's all.

If a sample GPA_Detail.dat file has not been provided for you, in a text editor (notepad, your C++ editor, etc.) simply type some sample lines like:

f 2.99
m 3.2
m 2.7
f 3.85
m 1.87
.....

Be sure to end with a return ( <enter> key )

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.