GPA Calculation Program/User defined Functions

Jul 30, 2011 at 5:32pm
I was given an assignment with the following parameters, basically five user defined functions. The output file should display the average male and female gpa but mine is whacky. Any tips would be appreciated...

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.
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


Here's my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void openFiles (ifstream&, ofstream&);
void initialize (ifstream&, ofstream&);
void sumGrades (float&, float&, ifstream&, ofstream&, int&, int&);
void averageGrade (int&, int&, float&, float&, ifstream&, ofstream&);
void printResults (int&, int&, float&, float&, ifstream&, ofstream&, float&, float&);

int _tmain(int argc, _TCHAR* argv[])
{
	ifstream in ;
	ofstream out ;
	openFiles (in, out) ;
	initialize (in , out) ;
	cout << "Processing grades "<< endl;      
	cout << "Output data is in file gpaAVG.txt " << endl;      
	cout << " "<< endl;      
	system("pause");
	return 0;
}

void openFiles(ifstream& in, ofstream& out) 
{
	in.open("student.txt");
	out.open("gpaAvg.txt");

	out << fixed << showpoint;
	out << setprecision(2);

	initialize(in , out);
}

void initialize(ifstream& in, ofstream& out) 
{
	char gender;
	float GPA;
	string line;
	int countFemale = 0;
	int countMale = 0;
	float sumFemaleGPA = 0;
	float sumMaleGPA = 0;

while(in >> gender >> GPA){
}
		
		if (gender == 'F')  
		{
			countFemale++; 
			sumFemaleGPA = sumFemaleGPA + GPA;
		}

		if (gender == 'M') 
		{ 
			countMale++;
			sumMaleGPA = sumMaleGPA + GPA;
		}
	sumGrades(sumFemaleGPA, sumMaleGPA, in, out, countFemale, countMale);
	averageGrade(countFemale, countMale, sumFemaleGPA, sumMaleGPA, in, out);
}


void sumGrades(float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out, int& countFemale, int& countMale) 
{
	out << "Sum female GPA: " << sumFemaleGPA << endl;
	out << "Sum male GPA: " << sumMaleGPA << endl;
}

void averageGrade(int& countFemale, int& countMale, float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out) 
{
	float averageFemaleGPA = sumFemaleGPA / countFemale;
	float averageMaleGPA = sumMaleGPA / countMale;
	printResults(countFemale, countMale, sumFemaleGPA, sumMaleGPA, in, out, averageFemaleGPA, averageMaleGPA);
}

void printResults(int& countFemale, int&countMale, float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out, float& averageFemaleGPA, float& averageMaleGPA) 
{
	
	out << "Female count: " << countFemale << endl;
	out << "Male count: " << countMale << endl;
	out << "Average female GPA: " << averageFemaleGPA << endl;
	out << "Average male GPA: " << averageMaleGPA;

	in.close();
	out.close();
}
 


My output txt file is the following. I cant figure out for the life of me why its doing this. I apologize if my mistakes seem painfully simple to you guys, this is my first CS class.

Sum female GPA: -107374176.00
Sum male GPA: 0.00
Female count: 1
Male count: 0
Average female GPA: -107374176.00
Average male GPA: -1.#J
Jul 30, 2011 at 8:27pm
Where is the loop to read the file? I see things that look like I am reading the file but I don't see something that looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void initialize(....)

while(!inFile.eof()) // I don't care how long my file is...
{
       inFile >> Gender;
       inFile >> GPA;

       // run my accumulators

       if(Gender == 'M')
       {
              maleCount++;
              maleGPA+=GPA;
       }
       else if(Gender == 'F')
       {
              femaleCount++;
              femaleGPA+=GPA;
        }
        
        studentCount++;
        studentGPA+= GPA;
        
        // do I need to display running totals, they go here....
}

// open my file for output
// report my data findings'
// close the file.
}// end of initialize 
Last edited on Jul 30, 2011 at 8:29pm
Topic archived. No new replies allowed.