Help

This is a discussion on Help within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream> #include <cstdlib> #include <cctype> using namespace std; void grades (ifstream& in, ofstream& out); double avg ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Help

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cctype>
    
    using namespace std;
    
    void grades (ifstream& in, ofstream& out);
    double avg (ifstream& in, ofstream& out);
    
    int main ()
    {
        char next;
        double average;
        
        ifstream fin;
        ofstream fout;
        
        fin.open ("F:\\CS 2\\scores.txt");
        
        if (fin.fail())
        {
           cout << "File failed to open.\n";
           system ("pause");
           exit (1);
        }
        
        fout.open ("F:\\CS 2\\grades.txt");
        
        if (fout.fail())
        {
           cout << "File failed to open.\n";
           system ("pause");
           exit (1);
        }
        
       
        grades (fin, fout);
        
         
        
        fin.close ();
        fout.close ();
        system ("pause>nul");
    }
    
    
    void grades(ifstream& in, ofstream& out)
    {
          char next;
          double average;
          
          while (! in.eof())
          {
          in.get(next);
          
          while (next != '\n')
          {
                cout << next;
                out << next;        
                in.get (next);
          }
          average = avg (in, out);
         
          cout << ' ' << average;
          out << ' ' << average;
          }
          
    }
       
    double avg (ifstream& in, ofstream& out)  
    {
          string last, first;
          double score [10], average = 0;
          int i;
          
          out.setf(ios::fixed);
          out.setf(ios::showpoint);
          out.precision(2);
          
           in >> last >> first;       
           for (i = 0; i < 10; i++)
           in >> score[i];
           
           
           average=(score[0]+score[1]+score[2]+score[3]+score[4]+score[5]+score[6]+score[7]+score[8]+score[9])/10;
           
           
           
           return (average);
    }

    It keeps returning the average as 43.90, which is the average of the scores for the next student. I don't know why though. Can somebody help me please?

    Here is the input:

    Lo Ma 1 2 3 4 5 6 7 8 9 10
    George Michelle 12 23 34 54 65 7 98 23 100 23
    Frat Brat 12 12 12 12 12 12 12 12 12 12
    Bore More 23 4 56 6 34 4 2 6 8 9

    The out put is:
    Lo Ma 1 2 3 4 5 6 7 8 9 10 43.90 12.00 15.20
    Last edited by Matsuya; 05-27-2009 at 10:24 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How about printing the scores as you read them in, to see what you are actually averaging.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    You aren't processing your input well. The grades function reads and outputs the first line of data (for Lo Ma). Then the avg function is called which reads the next line of data (for George Michelle) and calculates the average based on that person's scores (not Lo Ma's). When you return to the grades function to print the average, you going to end up printing George Michelle's average and not Lo Ma's.

    BTW, you should be #including the <string> header.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by hk_mp5kpdw View Post
    You aren't processing your input well. The grades function reads and outputs the first line of data (for Lo Ma). Then the avg function is called which reads the next line of data (for George Michelle) and calculates the average based on that person's scores (not Lo Ma's). When you return to the grades function to print the average, you going to end up printing George Michelle's average and not Lo Ma's.

    BTW, you should be #including the <string> header.
    So the average function picks up where the grades function left off?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by matsp View Post
    How about printing the scores as you read them in, to see what you are actually averaging.

    --
    Mats
    It's averaging the next students scores, but I don't know how to fix that.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Matsuya View Post
    So the average function picks up where the grades function left off?
    Indeed it does. You are using the same file, which only has one position where it reads from. You should read data ONCE.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by matsp View Post
    Indeed it does. You are using the same file, which only has one position where it reads from. You should read data ONCE.

    --
    Mats
    Is there a way to make it go back to the beginning of the file?
    Last edited by Matsuya; 05-28-2009 at 09:36 AM.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    in.get(next);
          
          while (next != '\n')
          {
                cout << next;
                out << next;        
                in.get (next);
          }
    What would you say is the purpose of this code?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by anon View Post
    Code:
    in.get(next);
          
          while (next != '\n')
          {
                cout << next;
                out << next;        
                in.get (next);
          }
    What would you say is the purpose of this code?
    It's to stop it at the end of the line so I can insert the average before going on to the next student.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    10
    Never mind, I've figured it out. Thank you.

Popular pages Recent additions subscribe to a feed

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