Thread: im stuck: void getscore should read from a file

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    19

    im stuck: void getscore should read from a file

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    // Function prototype
    void getScore(int &);
    void calcAverage(int, int, int, int, int);
    int findLowest(int, int, int, int, int);
    int grades(int, int, int, int, int);
    int main()
    {
        ifstream grades;
        grades.open("grades.txt");
    
        int testScr1, testScr2, testScr3, testScr4, testScr5;
    
    
        getScore(testScr1);
        getScore(testScr2);
        getScore(testScr3);
        getScore(testScr4);
        getScore(testScr5);
    
        calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5);
    
        grades.close();
    
        return 0;
    }
    
    void getScore(int &score)
    {
    
    }
    
    
    void calcAverage(int s1, int s2, int s3, int s4, int s5)
    {
        int sum;
        int      lowest;
        double average;
    
    
        lowest = findLowest(s1, s2, s3, s4, s5);
    
        sum = s1 + s2 + s3 + s4 + s5 - lowest;
        average = sum / 4.0;
    
        cout << setw(4) << fixed << showpoint << setprecision(2);
        cout << "The avergae of the four highest scores are: " << average << endl;
    }
    
    int findLowest(int s1, int s2, int s3, int s4, int s5)
    {
        int lowest = s1;
        
        cout << "The lowest test score is: " << lowest << endl;
    
        return lowest;
    }

    Void getscore() should read a score from the file and store it in reference parameter variable. This function should be called by main for each of the five scores to be entered. I dont know how to do this?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What do you not understand? How to read from a file? How to store something in a reference variable?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    Void getscore() should read a score from the file and store it in reference parameter variable. This function should be called by main for each of the five scores to be entered. I dont know how to do this?

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    Im starting to think that the getscore is not doing anything..just taking up space. How do I get the function to be called by main for each number?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's already being called:

    getScore(testScr1);
    getScore(testScr2);
    getScore(testScr3);
    getScore(testScr4);
    getScore(testScr5);

    So I still fail to see exactly what part of the process you are having trouble with? Have you tried to make something work?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OP: I think you need to read about fstream and understand how it works in order to do what you want. I think the ifstream object you have in main will have to be added to the parameter list of getscore() as a reference.
    C++ File I/O Tutorial - Cprogramming.com
    Last edited by whiteflags; 06-30-2014 at 02:29 PM.

  7. #7
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    // Function prototype
    void getScore(int &score);
    void calcAverage(int, int, int, int, int);
    int findLowest(int, int, int, int, int);
    int grades(int, int, int, int, int);
    int main()
    {
        ifstream inFile;
        inFile.open("grades.txt");
    
        int testScr1, testScr2, testScr3, testScr4, testScr5;
    
    
        getScore(testScr1);
        getScore(testScr2);
        getScore(testScr3);
        getScore(testScr4);
        getScore(testScr5);
    
        calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5);
    
        inFile.close();
    
        return 0;
    }
    
    void getScore(int &score, ifstream& inFile)
    {
    
        inFile >> score;
    }
    
    
    void calcAverage(int s1, int s2, int s3, int s4, int s5)
    {
        int sum;
        int      lowest;
        double average;
    
    
        lowest = findLowest(s1, s2, s3, s4, s5);
    
        sum = s1 + s2 + s3 + s4 + s5 - lowest;
        average = sum / 4.0;
    
        cout << setw(4) << fixed << showpoint << setprecision(2);
        cout << "The avergae of the four highest scores are: " << average << endl;
    }
    
    int findLowest(int s1, int s2, int s3, int s4, int s5)
    {
        int lowest = s1;
        
        cout << "The lowest test score is: " << lowest << endl;
    
        return lowest;
    }
    I changed a few things and yes I tried to run it..Now I get errors. I am trying to get the program to read a file with 5 test scores in it and then tell me which one will be dropped and what the average is of the remaining 4

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I changed a few things and yes I tried to run it..Now I get errors.
    Good! Progress is good! What are the errors? You should try to use the errors the compiler gives you to your advantage. In this case, the problem with your code is obvious, but it won't always be that way. People like me need to know what the errors are in order to fix them, just like you would need to know what the errors are to fix it yourself. Try to copy and paste the compiler's exact output.

    In a sentence, the function definition has to match the prototype.
    Code:
    //You declared the prototype like this:
    void getScore(int &score);
    //You defined it like this:
    void getScore(int &score, ifstream& inFile)
    {
     
        inFile >> score;
    }
    //Then finally you called it like this:
        getScore(testScr1);
        getScore(testScr2);
        getScore(testScr3);
        getScore(testScr4);
        getScore(testScr5);
    You changed the definition of getscore(), so you should change the prototype and lines that you call it on before you compile.
    Code:
    void getScore(int& score, ifstream& inFile);
    
    void getScore(int& score, ifstream& inFile)
    {
        inFile >> score;
    }
    
    getScore(testScore1, inFile);
    getScore(testScore2, inFile);
    .
    .
    .
    Last edited by whiteflags; 06-30-2014 at 03:18 PM.

  9. #9
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    Error 1 error C2660: 'getScore' : function does not take 2 arguments c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 19 1 test2
    Error 2 error C2660: 'getScore' : function does not take 2 arguments c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 20 1 test2
    Error 3 error C2660: 'getScore' : function does not take 2 arguments c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 21 1 test2
    Error 4 error C2660: 'getScore' : function does not take 2 arguments c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 22 1 test2
    Error 5 error C2660: 'getScore' : function does not take 2 arguments c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 23 1 test2

  10. #10
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    i did what u said and now i am showing u the new errors

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I pretty much explained this problem. You defined the getScore() function to take an int reference and an ifstream reference. The prototype has to match that. Plus, where you call getscore(), it has to take arguments in the form of variables that match those types.

    Like I showed you in the other post, code like this should compile for you:
    Code:
    void getScore(int& score, ifstream& inFile);
     
    void getScore(int& score, ifstream& inFile)
    {
        inFile >> score;
    }
     
    // Now in main...
    getScore(testScr1, inFile);
    getScore(testScr2, inFile);
    getScore(testScr3, inFile);
    getScore(testScr4, inFile);
    getScore(testScr5, inFile);
    Make sure everything matches.

  12. #12
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    yes it does compile but it returns back bogus #s.. None of which are the scores that its suppose to read..one of the numbers come back a negative.

  13. #13
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    // Function prototype
    void getScore(int& score, ifstream& inFile);
    void calcAverage(int, int, int, int, int);
    int findLowest(int, int, int, int, int);
    int grades(int, int, int, int, int);
    int main()
    {
        ifstream inFile;
        inFile.open("grades.txt");
    
        int testScr1, testScr2, testScr3, testScr4, testScr5;
    
    
        getScore(testScr1,inFile);
        getScore(testScr2,inFile);
        getScore(testScr3,inFile);
        getScore(testScr4,inFile);
        getScore(testScr5,inFile);
    
        calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5);
    
        inFile.close();
    
        return 0;
    }
    
    void getScore(int& score, ifstream& inFile)
    {
    
        inFile >> score;
    }
    
    
    void calcAverage(int s1, int s2, int s3, int s4, int s5)
    {
        int sum;
        int      lowest;
        double average;
    
    
        lowest = findLowest(s1, s2, s3, s4, s5);
    
        sum = s1 + s2 + s3 + s4 + s5 - lowest;
        average = sum / 4.0;
    
        cout << setw(4) << fixed << showpoint << setprecision(2);
        cout << "The avergae of the four highest scores are: " << average << endl;
    }
    
    int findLowest(int s1, int s2, int s3, int s4, int s5)
    {
        int lowest = s1;
        
        cout << "The lowest test score is: " << lowest << endl;
    
        return lowest;
    }
    this is the new code and it returns bogus numbers...what it should be reading is 2 digit grades and it does not or it returns something that I dont know how to fix

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just in case, it would be best if you posted the contents of the file you're reading.
    Also make sure you can actually read the file. It might be that you failed to open the file. You can test the stream to see if it is in an error state or not. If it fails to open, it will be in an error state:

    ifstream inFile;
    inFile.open("grades.txt");
    if (!inFile) // This means the file is in an error state
    do something;

    Also, this:

    ifstream inFile;
    inFile.open("grades.txt");

    can be simplified to this:

    ifstream inFile("grades.txt");
    Last edited by Elysia; 06-30-2014 at 06:27 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    all it is a notpad txt with 5 different grades in it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  2. Replies: 2
    Last Post: 04-04-2012, 08:16 AM
  3. Replies: 3
    Last Post: 09-06-2009, 01:48 PM
  4. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  5. Replies: 16
    Last Post: 01-04-2007, 03:38 PM