Program Won't Print To Output File?

Basically, I can't get the average GPAs for either male or female to output to a txt file. I've been tinkering with this thing for hours but to no avail. I know there has to be something wrong with my printResults function but can't figure out for the life of me what it is. Would really appreciate some help...

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
// GpaDraft2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

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


int _tmain(int argc, _TCHAR* argv[])
{
	ifstream in;
	ofstream out;
	openFiles(in, out);
	return 0;
}
void openFiles(ifstream& in, ofstream& out) {
	in.open("student.txt");
	out.open("avgGpa.txt");
	out << fixed << showpoint;
	out << setprecision(2);
	initialize(in, out);
}

void initialize(ifstream& in, ofstream& out) {
	char gender;
	double GPA;
	string line;
	int countFemale = 0;
	int countMale = 0;
	double sumFemaleGPA = 0;
	double sumMaleGPA = 0;
	while (!in.eof()) {
		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(double& sumFemaleGPA, double& sumMaleGPA, ifstream& in, ofstream& out, int& countFemale, int& countMale) {
	out << "Total Sum of GPA Grades: " << sumFemaleGPA + sumMaleGPA << endl;
}

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


}

void printResults(int& countFemale, int&countMale, double& sumFemaleGPA, double& sumMaleGPA, ifstream& in, ofstream& out, double& averageFemaleGPA, double& averageMaleGPA) {
	out << "Total Females: " << countFemale << endl;
	out << "Total Males: " << countMale << endl;
	out << "Female Sum GPA: " << sumFemaleGPA << endl;
	out << "Male Sum GPA: " << sumMaleGPA << endl;
	out << "Female Average GPA: " << averageFemaleGPA << endl;
	out << "Male Average GPA: " << averageMaleGPA; 
	in.close();
	out.close();
}


I have a similar problem. no solutions?
Few questions;

a) I'm assuming there are no errors. Is that correct?

b) Try deleting the output file. Does the program create it, or does it not reappear?

c) If it appears: is it completely empty, or is there some content?

Also, would it be possible to provide a sample of student.txt, so I can try and run it myself?

[edit]

By the way, I'm not sure about fixed, but setprecision() is used as such:
out.setprecision(N)

Also, I'm not sure why in is passed on beyond initizalize(). I'd put the close() somewhere else.
Last edited on
Does student.txt already exist?

try adding the ios::app flag to the output file:

ofstream out("file", ios::app);

EDIT: If you still don't manage, post the student.txt file so that I can take a look at it.
Last edited on
@Gaminic:

Yes, the are no compiling errors. The program creates the output file, but it is completely blank.

@eidge: I will try adding that flag to the output file later. I am work right now so I don't have access to a compiler.

Here's the student.txt file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
F, 3.4
F, 3.7
M, 2.8
M, 3.8
F, 3.1
F, 2.7
F, 3.34
F, 2.92
F, 2.76
M, 2.67
M, 3.23
M, 3.63
F, 3.47
M, 2.90
F, 2.87
M, 3.01
F, 3.14
F, 2.88
F, 3.41
M, 3.50
M, 3.22
M, 2.98



Your extraction operators look wrong!

in >> gender >> GPA;

with

1
2
char gender;
double GPA;


is not going to work with

F, 3.4

because of the comma.

Andy

P.S. And operator== is case sensitive!

Other than that (a few small changes) your program runs:

Total Sum of GPA Grades: 72.41
Total Females: 12
Total Males: 11
Female Sum GPA: 37.69
Male Sum GPA: 34.72
Female Average GPA: 3.14
Male Average GPA: 3.16

(based on posted data)

I would add code to trap error, like the file failing to open.

Also, your function really need to be rearranged a bit: a function's name should say what it actually does, it should only do what it says it does, and it should only perform once task.

e.g. printResults should not close the files, open files should not initialize, and initialize should not process. But you just need to move a few function calls to get the order right!
Topic archived. No new replies allowed.