stallion5000
asked on
I am calculating average GPA from an input file and then outputting the average to an outfile, but I'm getting weird output numbers.
I am calculating average GPA from an input file and then outputting the average to an outfile, but I'm getting weird output numbers.
here is my sample input
gender gpa
f 3.50
m 2.60
m 2.90
f 2.40
f 3.00
m 3.20
f 4.00
m 2.45
and here is my program in C++
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
main()
{
int numM=0;
int numF=0;
double averageF= 0.00;
double averageM = 0.00;
double gpa = 0.00;
double totalf = 0;
double totalm = 0;
char gender;
ifstream infile;
ofstream outfile;
infile.open ("c:\\english.txt");
outfile.open ("c:\\english1.txt");
outfile << fixed << showpoint;
outfile << setprecision(2);
infile >> gender >> gpa;
while (infile)
{
switch (gender)
{
case 'F':
case'f': totalf = totalf + gpa;
numF++;
break;
case 'M':
case 'm': totalm = totalm + gpa;
numM++;
break;
}
infile >> gender >> gpa;
}
averageF = totalf / numF;
averageM = totalm / numM;
outfile << averageF; outfile << endl;
outfile << averageM;
}
now here is my output
-1.#J
-1.#J
I need know what the hell that is, because its not what I should be getting. Can someone please tell me what I am doing wrong.
here is my sample input
gender gpa
f 3.50
m 2.60
m 2.90
f 2.40
f 3.00
m 3.20
f 4.00
m 2.45
and here is my program in C++
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
main()
{
int numM=0;
int numF=0;
double averageF= 0.00;
double averageM = 0.00;
double gpa = 0.00;
double totalf = 0;
double totalm = 0;
char gender;
ifstream infile;
ofstream outfile;
infile.open ("c:\\english.txt");
outfile.open ("c:\\english1.txt");
outfile << fixed << showpoint;
outfile << setprecision(2);
infile >> gender >> gpa;
while (infile)
{
switch (gender)
{
case 'F':
case'f': totalf = totalf + gpa;
numF++;
break;
case 'M':
case 'm': totalm = totalm + gpa;
numM++;
break;
}
infile >> gender >> gpa;
}
averageF = totalf / numF;
averageM = totalm / numM;
outfile << averageF; outfile << endl;
outfile << averageM;
}
now here is my output
-1.#J
-1.#J
I need know what the hell that is, because its not what I should be getting. Can someone please tell me what I am doing wrong.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I got it, little stupid mistakes. I'll split the points, thanks for your responses
Even g++ gives the result as expected.
3.23
2.79
Cheers!