2010-09-22, 06:21 AM
First, convert your main() into a function like update_gpa(float gpa, int* gender_count, float* total_gpa). Basically, all this function does is updating the scores. Once you did that, recode your main() to call this function.
Once that's done, you will need to use a file stream reader (which I totally forgot the syntax for) and I think there's a function readline to read in a line in the file. As you can see, line[0] is the gender, and the rest is a float. Substring will do the trick here.
Your main() will be something like this:
for each line in the file, gender = line[0], gpa = convert to float(line[1..n])
if gender is m, pass in the gpa, the male total, the male gpa total into update_gpa
otherwise, pass in the gpa, the female total, the female total gpa into update_gpa
IMO, that's clean modular programming.
To make this OOP, firstly, you need a class calls GpaManager, which has 4 vars, male_total, female_total, male_total_gpa, female_total_gpa and perhaps 2 constants, MALE and FEMALE.
Then, you would need a public function calls update_gpa(char gender, &float gpa) and
protected function update_female_gpa(&float gpa)
protected function update_male_gpa(&float gpa)
Note that you're only going to read the GPA, so it's OK to pass the reference.
In update_gpa, it does the if statement and calls the male/female version of the function.
you can also do a toString() function for the class GpaManager, so that you can do something like cout << GpaManager.
That's pretty much does all the GPA logics.
Now, you can do something like a StdinGpaManager, which reads the data in from the std input and/or a FileGpaManager, which reads the data in from the file.
Once that's done, you will need to use a file stream reader (which I totally forgot the syntax for) and I think there's a function readline to read in a line in the file. As you can see, line[0] is the gender, and the rest is a float. Substring will do the trick here.
Your main() will be something like this:
for each line in the file, gender = line[0], gpa = convert to float(line[1..n])
if gender is m, pass in the gpa, the male total, the male gpa total into update_gpa
otherwise, pass in the gpa, the female total, the female total gpa into update_gpa
IMO, that's clean modular programming.
To make this OOP, firstly, you need a class calls GpaManager, which has 4 vars, male_total, female_total, male_total_gpa, female_total_gpa and perhaps 2 constants, MALE and FEMALE.
Then, you would need a public function calls update_gpa(char gender, &float gpa) and
protected function update_female_gpa(&float gpa)
protected function update_male_gpa(&float gpa)
Note that you're only going to read the GPA, so it's OK to pass the reference.
In update_gpa, it does the if statement and calls the male/female version of the function.
you can also do a toString() function for the class GpaManager, so that you can do something like cout << GpaManager.
That's pretty much does all the GPA logics.
Now, you can do something like a StdinGpaManager, which reads the data in from the std input and/or a FileGpaManager, which reads the data in from the file.

