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
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;

      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; ("c:\\english.txt"); ("c:\\english1.txt");

      outfile << fixed << showpoint;
      outfile << setprecision(2);

      infile >> gender >> gpa;

      while (infile)
            switch (gender)
            case 'F':
            case'f': totalf = totalf + gpa;
            case 'M':
            case 'm': totalm = totalm + gpa;
            infile >> gender >> gpa;
      averageF = totalf / numF;
      averageM = totalm / numM;

      outfile << averageF; outfile << endl;
      outfile << averageM;

now here is my output


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.
Question by:stallion5000
    LVL 15

    Assisted Solution

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


    What compiler are you using?
    LVL 55

    Accepted Solution

    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.
    LVL 4

    Expert Comment

    Hi stallion5000,

    Even g++ gives the result as expected.



    Author Comment

    I got it, little stupid mistakes. I'll split the points, thanks for your responses

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now