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.
stallion5000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

efnCommented:
I tried your program with Microsoft Visual C++ 6.0 and failed to replicate your result.  The output I got was

3.23
2.79

What compiler are you using?
0
Jaime OlivaresSoftware ArchitectCommented:
I have good results with Dev C++ also.
Are you including 'gender          gpa' column titles or a white space, if so, then your program will fail as described.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pankajtiwaryCommented:
Hi stallion5000,

Even g++ gives the result as expected.

3.23
2.79

Cheers!
0
stallion5000Author Commented:
I got it, little stupid mistakes. I'll split the points, thanks for your responses
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.