calculate average from a file

This is a discussion on calculate average from a file within the C++ Programming forums, part of the General Programming Boards category; same program as before. reads in a text file. in the text file there are males and females and scores ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    calculate average from a file

    same program as before. reads in a text file. in the text file there are males and females and scores for them.
    I would like to calculate the average for all the scores for the males or females.
    what would be the best way to go about this?
    I am sure i would have to first find all the females and then add the scores up and didvide by the number of females. but not sure how to go about that?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    char name[25];
    char gender;
    char code[5];
    int score;
    int count = 0;
    
    char reply;
    ifstream infile;
    string myfile;
    
    //char process[200];
    
    ifstream inputFile;
    
    cout << "Enter file name with the extension(Example: scores.txt): " << endl;
    
    cin >> myfile;
    
    inputFile.open (myfile.c_str(), ios::in);
    
    if(!inputFile)
     {
      cerr << "Can't open input file " << myfile << endl;
      cin >> reply;
      exit(1);
    }
    
    
    
    while(!inputFile.eof())
    {
    inputFile >> name;
    count++;
    cout << name;
    inputFile >> gender;
    cout << gender;
    inputFile >> code;
    cout << code;
    inputFile >> score;
    cout << score << endl;
    
    //inputFile.getline(process,200);
    
    }
    
    
     }
     
    
     cin >> reply;
      return 0;
    }
    output:
    M = male
    F= female
    CC = comunity college.
    just to clarify

    Code:
    Enter file name with the extension(Example: scores.txt):
    scores.txt
    Bailey    M CC  68
    Harrison    F CC  71
    Grant    M UN  75
    Peterson    F UN  69
    Hsu    M UN  79
    Bowles    M CC  75
    Anderson    F UN  64
    Nguyen    F CC  68
    Sharp    F CC  75
    Jones    M UN  75
    McMillan    F UN  80
    Gabriel    F UN  62
        F     62
    not sure why it's picking up the last F 62 thats from the last name Gabriel.

    thank you!

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    No need to read everything into memory.

    Keep 4 variables - num_females, num_males, sum_males, sum_females.

    Read the file. For every entry, add the score to either sum_females or sum_males, and increment num_females or num_males.

    Then, avg is just "sum / num".

    Oh and, indent properly.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mrsirpoopsalot View Post
    not sure why it's picking up the last F 62 thats from the last name Gabriel.

    thank you!
    And this is why you don't use .eof() to control a loop.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I am getting the same results when i do this :
    Code:
    while(inputFile)
    {
    inputFile >> name;
    cout << name;
    inputFile >> gender;
    cout << setw(5) << gender;
    inputFile >> code;
    cout << setw(3) << code;
    inputFile >> score;
    cout << setw(4) << score << endl;
    
    //inputFile.getline(process,200);
    
    }
    output:
    Code:
    Enter file name with the extension(Example: scores.txt):
    scores.txt
    Bailey    M CC  68
    Harrison    F CC  71
    Grant    M UN  75
    Peterson    F UN  69
    Hsu    M UN  79
    Bowles    M CC  75
    Anderson    F UN  64
    Nguyen    F CC  68
    Sharp    F CC  75
    Jones    M UN  75
    McMillan    F UN  80
    Gabriel    F UN  62
        F     62
    that last F 62 should not be there..

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    What is your current code? Also, what is the sample input?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    my current code:
    I will fix the identing later.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    int num_females = 0;
    int num_males = 0; 
    int sum_males; 
    int sum_females;
    
    char name[25];
    char gender;
    char code[5];
    int score;
    int count = 0;
    
    char reply;
    ifstream infile;
    string myfile;
    
    //char process[200];
    
    ifstream inputFile;
    
    cout << "Enter file name with the extension(Example: scores.txt): " << endl;
    
    cin >> myfile;
    
    inputFile.open (myfile.c_str(), ios::in);
    
    if(!inputFile)
     {
      cerr << "Can't open input file " << myfile << endl;
      cin >> reply;
      exit(1);
    }
    
    
    
    while(inputFile)
    {
    inputFile >> name;
    cout << name;
    inputFile >> gender;
    if(gender == 'F')
    {
    	num_females++;
    }
    else
    {
    	num_males++;
    }
    cout << setw(5) << gender;
    inputFile >> code;
    cout << setw(3) << code;
    inputFile >> score;
    cout << setw(4) << score << endl;
    
    //inputFile.getline(process,200);
    
    }
    
    
     cout << "number of males = " << num_males;
     
    
     cin >> reply;
      return 0;
    }
    output:
    Code:
    Enter file name with the extension(Example: scores.txt):
    scores.txt
    Bailey    M CC  68
    Harrison    F CC  71
    Grant    M UN  75
    Peterson    F UN  69
    Hsu    M UN  79
    Bowles    M CC  75
    Anderson    F UN  64
    Nguyen    F CC  68
    Sharp    F CC  75
    Jones    M UN  75
    McMillan    F UN  80
    Gabriel    F UN  62
        F     62
    number of males = 5
    that last F 62 is part of Gabriel and should not be there..
    here is a copy of scores.txt

    Code:
    Bailey           M CC 68
    Harrison         F CC 71
    Grant            M UN 75
    Peterson         F UN 69
    Hsu              M UN 79
    Bowles           M CC 75
    Anderson         F UN 64
    Nguyen           F CC 68
    Sharp            F CC 75
    Jones            M UN 75
    McMillan         F UN 80
    Gabriel          F UN 62
    Last edited by mrsirpoopsalot; 01-20-2009 at 12:24 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by mrsirpoopsalot
    I will fix the identing later.
    You could indent while you code. If you want such a lazy approach to indentation, use a source code formatting tool (your text editor/IDE may even come with one, and it probably can also be configured to help you indent while you code).

    Quote Originally Posted by mrsirpoopsalot
    that last F 62 is part of Gabriel and should not be there..
    here is a copy of scores.txt
    I am unable to duplicate your output. However, I do get an output with this line repeated:
    Code:
    Gabriel    F UN  62
    The reason is as before: you need to control the loop with the return value of the functions used to read. In this case you could write:
    Code:
    while (inputFile >> name >> gender)
    {
        cout << name;
        if (gender == 'F')
        {
            num_females++;
        }
        else
        {
            num_males++;
        }
        cout << setw(5) << gender;
        inputFile >> code;
        cout << setw(3) << code;
        inputFile >> score;
        cout << setw(4) << score << endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    well if i do something like this
    Code:
    while(inputFile >> name )
    {
    inputFile >> name;
    cout << name;
    inputFile >> gender;
    if(gender == 'F')
    {
    	num_females++;
    }
    else
    {
    	num_males++;
    }
    cout << setw(5) << gender;
    inputFile >> code;
    cout << setw(3) << code;
    inputFile >> score;
    cout << setw(4) << score << endl;
    it wont display the name.. or if i add more to the loop condition it wont display those results.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by mrsirpoopsalot
    well if i do something like this
    Compare your code and mine. The critical difference is that you read into name twice on each iteration.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    laserlight,

    thank you it works, but im a little confused by what the condition is.
    why cant we just use name and why name and gender?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by mrsirpoopsalot
    thank you it works, but im a little confused by what the condition is.
    The underlying problem is that the EOF condition is only set when an attempt is made to read, and finds that it has reached the end of the file. This is why your original loop runs one extra time: the condition is tested and found that the EOF bit is not set, but actually the file has been completely read.

    Quote Originally Posted by mrsirpoopsalot
    why cant we just use name and why name and gender?
    To avoid the problem that you faced originally, it is enough to just read into name in the loop condition. The advantage of including gender, and even including code and score, is that the loop will terminate if there is invalid input, or if the last line is incomplete.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    thank you
    Last edited by mrsirpoopsalot; 01-20-2009 at 01:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. I have a function and I need multiple averages returned
    By tommy69 in forum C++ Programming
    Replies: 20
    Last Post: 04-13-2004, 11:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21