Program for calculating GPA (help please)

Hello, I am very new to C++ and on that same note, even newer to void functions, and using reference parameters. The assignment is to create a program that will read an input file with student gender followed by their GPA in the following format:

f 4.0
m 3.0
f 4.0
m 2.5
f 1.7


This is just an example, there is no predefined number of entry lines so the program needs to test for the end of file.

Bottom line: Program reads input file. Program has AT LEAST these functions:
function for initializing (countFemale, countMale,sumFemaleGPA,sumMaleGPA)
function for opening the input/output files and formatting the output
function for calculating the sum for both male and female GPAs
function for calculating the average GPA for both male and female
function printResults to output the results to the output file "out.txt"

Conditions: NO GLOBAL VARIABLES CAN BE USED, we are to use proper referencing in and out of functions to obtain values for variables.

Lastly, before I post the code. I realize this program, as it stands now, probably has many reference errors, and/or improper usage. I am hoping not just a solution to this problem, but to learn from my mistakes so that I can truly understand void functions, functions taking parameters, and reference vs. value better. I appreciate any and all help, it will be much appreciated!

Here is my program 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*********************************************************************
    Name: Daniel Wilson
    Project: Lab_6_exercise_6 (GPA Problem)
    Date of Coding: 03/18/2011
    Course: C++ IST-163-201
    Instructor: Brian Morgan
    Help Recieved: c++ textbook,in-class discussions

**********************************************************************/

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;

//function prototypes
int openFiles();
void initialize(int& countFemale,int& countMale,double& sumFemaleGPA,double& sumMaleGPA);
void averageGrade(int countFemale, int countMale, double sumFemaleGPA, double sumMaleGPA,double& averageFemaleGPA,double& averageMaleGPA);
void printResults(double averageFemaleGPA,double averageMaleGPA);
int getSex(istream& in);
int getGPA(istream& in);
void sumGrades(int sex,double gpa, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA);


int main()
{
    int countFemale;
    int countMale;
    double sumFemaleGPA;
    double sumMaleGPA;
    double averageFemaleGPA;
    double averageMaleGPA;
    int sex;
    double gpa;
    ifstream fin;
	ofstream fout;
    
    initialize(countFemale,countMale,sumFemaleGPA,sumMaleGPA);
    openFiles();

	sex = (getSex(fin));
    gpa = getGPA(fin);
	while (!fin.eof())
	{
        sumGrades(sex,gpa,countFemale,countMale,sumFemaleGPA,sumMaleGPA);
		sex = getSex(fin);
        gpa = getGPA(fin);
	}
    
    averageGrade(countFemale,countMale,sumFemaleGPA,sumMaleGPA,averageFemaleGPA,averageMaleGPA);

	printResults(averageFemaleGPA,averageMaleGPA);

	return 0;
}

void initialize(int& countFemale,int& countMale,double& sumFemaleGPA,double& sumMaleGPA)
{
    countFemale = 0;
    countMale = 0;
    sumFemaleGPA = 0.0;
    sumMaleGPA = 0.0;
}


int openFiles()
{
    ifstream fin; //input stream variable for data
    ofstream fout; //output stream variable

    fin.open("inputdata.txt");

    if (!fin)
    {
        cout << "Unable to open the input file." << endl;
        cout << "This program terminates." << endl;
        return 1;
    }

    fout.open("out.txt");
    fout << fixed << showpoint;
    fout << setprecision(2);
}

void sumGrades(int sex,double gpa, int countFemale, int countMale, double sumFemaleGPA, double sumMaleGPA)
{
    
    if(sex == 'f')
    {
        countFemale++;
        sumFemaleGPA = sumFemaleGPA + gpa;
    }
    else 
    {
        countMale++;
        sumMaleGPA = sumMaleGPA + gpa;
    }
}

void averageGrade(int countFemale, int countMale, double sumFemaleGPA, double sumMaleGPA,double& averageFemaleGPA,double& averageMaleGPA)
{
    
    averageFemaleGPA = sumFemaleGPA / countFemale;
    averageMaleGPA = sumMaleGPA / countMale;
}

void printResults(double averageFemaleGPA,double averageMaleGPA)
{
    fout << endl << "The average female GPA is " << averageFemaleGPA << "." << endl;
    fout << "The average male GPA is " << averageMaleGPA << "." << endl;
}

int getSex(istream& in)
{
	char sex;
	in >> sex;
    sex = static_cast<int>(sex);
    return sex;
}

int getGPA(istream& in)
{
	double gpa;
	in >> gpa;
	return gpa;
}
Also I forgot to mention:
I am receiving the following warnings/errors when compiling:
Warning 1 warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
Warning 2 warning C4715: 'openFiles' : not all control paths return a value

Error 3 error LNK2019: unresolved external symbol "void __cdecl sumGrades(int,double,int &,int &,double &,double &)" (?sumGrades@@YAXHNAAH0AAN1@Z) referenced in function _main

Error 4 fatal error LNK1120: 1 unresolved externals


Could you be more specific as to your problem? I can't speak for everyone but personally, I'd rather not look through your code and try to find errors.

The rule of thumb would be this:
Pass by reference if you want to modify the variable being based.
Otherwise, pass by value and you will get a copy.
Well, one specific question that I do have is about my function for obtaining the gender from the input file. Is the following set-up, or implemented correctly?

I have:
1
2
3
4
5
6
7
int getSex(istream& in)
{
	char sex;
	in >> sex;
    sex = static_cast<int>(sex);
    return sex;
}


I intend for this to grab the character, convert it to an int value, and then return that value so that I can test it against the int value of 'f' with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void sumGrades(int sex,double gpa, int countFemale, int countMale, double sumFemaleGPA, double sumMaleGPA)
{
    
    if(sex == 'f')
    {
        countFemale++;
        sumFemaleGPA = sumFemaleGPA + gpa;
    }
    else 
    {
        countMale++;
        sumMaleGPA = sumMaleGPA + gpa;
    }
}

Ah, I see you posted some errors...apparently you ninja'd my response. The linker is getting an error because your prototype for sumGrades() doesn't match the actual implementation.

Your first function doesn't do what you want; static_cast<int> does indeed create an int, but it is then assign to sex, which is a char, so it loses whatever the cast gained for you anyway.

There is no need to cast it to an int. Simply return a char and pass that to the different functions; you are comparing it to character literals anyway so there isn't any reason to store it as an integer.
Okay Zhuge, my program now runs without any errors, however, It is not running the program.

Here is my adjusted code (I'm getting closer):

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*********************************************************************
    Name: Daniel Wilson
    Project: Lab_6_exercise_6 (GPA Problem)
    Date of Coding: 03/18/2011
    Course: C++ IST-163-201
    Instructor: Brian Morgan
    Help Recieved: c++ textbook,in-class discussions

**********************************************************************/

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;

//function prototypes
int openFiles();
int getSex(istream& in);
double getGPA(istream& in);
void initialize(int& countFemale,int& countMale,double& sumFemaleGPA,double& sumMaleGPA);
void sumGrades(char sex,double gpa, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA);
void averageGrade(int countFemale, int countMale, double sumFemaleGPA, double sumMaleGPA,double& averageFemaleGPA,double& averageMaleGPA);
void printResults(double averageFemaleGPA,double averageMaleGPA);


int main()
{
    int countFemale;
    int countMale;
    double sumFemaleGPA;
    double sumMaleGPA;
    double averageFemaleGPA;
    double averageMaleGPA;
    char sex;
    double gpa;
    ifstream fin;
	ofstream fout;
    
    initialize(countFemale,countMale,sumFemaleGPA,sumMaleGPA);
    openFiles();

	sex = (getSex(fin));
    gpa = getGPA(fin);
	while (!fin.eof())
	{
        sumGrades(sex,gpa,countFemale,countMale,sumFemaleGPA,sumMaleGPA);
		sex = getSex(fin);
        gpa = getGPA(fin);
	}
    
    averageGrade(countFemale,countMale,sumFemaleGPA,sumMaleGPA,averageFemaleGPA,averageMaleGPA);

	printResults(averageFemaleGPA,averageMaleGPA);

	fin.close();
	fout.close();

	return 0;
}

int openFiles()
{
    ifstream fin; //input stream variable for data
    ofstream fout; //output stream variable

    fin.open("inputdata.txt");

    if (!fin)
    {
        cout << "Unable to open the input file." << endl;
        cout << "This program terminates." << endl;
        return 1;
    }

    fout.open("out.txt");
    fout << fixed << showpoint;
    fout << setprecision(2);
}

int getSex(istream& in)
{
	char sex;
	in >> sex;
    return sex;
}

double getGPA(istream& in)
{
	double gpa;
	in >> gpa;
	return gpa;
}

void initialize(int& countFemale,int& countMale,double& sumFemaleGPA,double& sumMaleGPA)
{
    countFemale = 0;
    countMale = 0;
    sumFemaleGPA = 0.0;
    sumMaleGPA = 0.0;
}



void sumGrades(char sex,double gpa, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA)
{
    
    if(sex == 'f')
    {
        countFemale++;
        sumFemaleGPA = sumFemaleGPA + gpa;
    }
    else 
    {
        countMale++;
        sumMaleGPA = sumMaleGPA + gpa;
    }
}

void averageGrade(int countFemale, int countMale, double sumFemaleGPA, double sumMaleGPA,double& averageFemaleGPA,double& averageMaleGPA)
{
    
    averageFemaleGPA = sumFemaleGPA / countFemale;
    averageMaleGPA = sumMaleGPA / countMale;
}

void printResults(double averageFemaleGPA,double averageMaleGPA)
{
    ofstream fout; //output stream variable
	
	fout << endl << "The average female GPA is " << averageFemaleGPA << "." << endl;
    fout << "The average male GPA is " << averageMaleGPA << "." << endl;
}
When it launches, it just says "Press any key to continue..."
Topic archived. No new replies allowed.