I am in desperate need of help. I have been working on this program for 10 hours now. It compiles just fine, and will run, but it does not work correctly. The purpose of the program is to read the gender and the corresponding GPA from a data file. Then calculate the average GPA of both genders. When I run the program it comes back 0.0 for both averages which is incorrect. I do not know what I am doing wrong.
The data file is formatted as so...
m 1.0
f 2.4
m 3.3
m 4.0
etc...
Subscribe to get a FREE chapter from Windows 7 The Missing Manual
This month, every Windows Secrets subscriber can download a one-chapter excerpt of Windows 7: The Missing Manual.Windows 7: The Missing Manual provides valuable information to help you overcome these difficulties in learning a new operating system. Subscribe today to download your free excerpt.
Welcome to the lounge and I hope I got your name right from hour handle?
Now I'm not a C++ guru but I used to be pretty good with C and I think your problem is that when you call your functions you are passing by value, i.e. passing the contents of a variable, when you want to be passing the address of that variable, passing by reference. When you pass by value the called routine, e.g. SumFemaleGPA has no way to pass the calculated value back to your Main procedure. If you pass by reference the called procedure uses the actual storage space of the variable declared in Main so the results are basically passed back to Main.
You have to be very careful in function based languages like C & C++ to be sure your functions can communicate by passing the correct type of information {values or addressed/references} to the called procedures. The thing to remember is that if you want the data changed by a function to be returned pass a reference {&variablename}. If the value being passed is only for the use of the function and you have no further use for it pass a value {variablename}
From a quick refresher look through "The Annotated C++ Reference Manual" this is what I think you want:
I'm not quite sure why you are using all the & after the variable types? Again, I've been away from this for 15 years or so...so I may be completely out of date.
Note: The different notation in how you call the function and how the function is declared.
I hope I haven't confused you, it's been a long time, and this helps you solve your problems.
in the sumGrades function you have a while loop. it needs to read
while (!inFile.eof())
instead of
while (inFile.eof());
inFile.eof() returns false unless it sees the endfile character so you need to use the not (!) character.In addition you don't put that semicolon there. i hope this helps!
EDIT: i corrected a mistake i made.