?
Solved

I am calculating average GPA from an input file and then outputting the average to an outfile, but I'm getting weird output numbers.

Posted on 2004-11-07
4
Medium Priority
?
1,027 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:stallion5000
4 Comments
 
LVL 15

Assisted Solution

by:efn
efn earned 750 total points
ID: 12519480
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 750 total points
ID: 12519730
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
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12520346
Hi stallion5000,

Even g++ gives the result as expected.

3.23
2.79

Cheers!
0
 

Author Comment

by:stallion5000
ID: 12520703
I got it, little stupid mistakes. I'll split the points, thanks for your responses
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question