If on the other hand, this was about solving the problem "in any form that works", the aforementioned solution is fine. But we have to look at what the poster is asking for, not just find "any solution".
>> ok, did and somewhat get it but I still don't understand why it gives the same answer for both m + f and now its more off than before on the answer?
Show your current code. You were on the right track by making the variable genderFromFile, you just didn't implement it correctly.
Does that code work for you?
What's actually wrong with the code? I don't understand. I get:
They're not the same!
hmm when I compile and run it , it gives me -1.#IND for m and when I run it and pick f it gives me the sane answser
I'm going to try making another program next week like this to learn it but right now I can't really think cause I haven't really been sleeping, yesterday I slept 1 hr and I've really been trying hard to get this program to work because I really need it >_<
Then there has to be something that breaks your code, either the input file or compiler optimization, because it works fine for me both on debug and release with the input file you mentioned in the beginning of the thread.
EDIT: Why don't you try the following code and reply with what output you get:
I added debug info to see if your program screws up somewhere, so post the outpost from the console window.Code:
string name, majorname, coursename;
double gpa = 0, mgpa = 0, fgpa = 0, averagef = 0, averagem = 0;
int mcounter = 0, fcounter = 0;
char gender = 0, show_gender = '\0';
//char genderFromFile = 0;
//char m = 0;
cout << "Hello there, What is your name?" << endl;
cout << "Welcome " << name << ".\n";
cout << "Please enter your degree your majoring in." << endl;
cout << "What the name of the course your taking for your major " << majorname << " ?\n";
cout << "What is your gender? Enter m for male or f for female." << endl;
cin >> show_gender;
//outData << "Person's name is " << name << ". And there degree major is " << majorname << ". The class course name for there degree major is " << coursename << ".\n";
while(inData >> gender >> gpa)
cout << "DEBUG INFO: gender = " << gender << ", gpa = " << gpa << endl;
if (gender == 'm')
mgpa += gpa;
else if (gender == 'f')
fgpa += gpa;
if (show_gender == 'm')
averagem = mgpa / mcounter; // Average GPA
cout << "The average for the male gpa is: " << averagem << endl;
else if (show_gender == 'f')
averagef = fgpa / fcounter; // Average GPA. Divide the total GPA by the number of entries found in the file.
cout << "the average for the female gpa is: " << averagef << endl;
omg! yes lol I have like a bunch of different program codes cpp files because every time I tried something new I would save it in a new folder and so I made this into a new folder and haden't moved the input file ..Thank you lol
sorry >_< lol I tried debugging it too ..then I reread the sentence you posted and then it hit me . danm! I forgot the file >_<
So, when you put in the input file, does it work?
EDIT: It might also be a good idea to check if the file is open after trying to open it. That way you check for errors, if the opening failed for some reason:
if ( !inData.is_open() )
cout << "ERROR: Could not open input file for reading!\n";
if ( !outData.is_open() )
cout << "ERROR: Could not open output file for writing!\n";
ok now I think this how you format your number to 2 decimal places and show .00?
changing the declaration part to 0.00 instead of 0? or am I close?
yes it worked :) . should I put that code before the if else stuff?
>> changing the declaration part to 0.00 instead of 0? or am I close?
Not really. You need to use an io manipulator in <iomanip>. Have you learned about any of those?
>> should I put that code before the if else stuff?
Checking the is_open() function should be done after opening the file and before using it.