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

  1. #31
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I also recommend you read the documentation for your compiler and increase the warning levels. Look at the warnings I get when I compile your code.

    main.cpp||In function ‘int main()’:|
    main.cpp|22|warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]|
    main.cpp|25|warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]|
    main.cpp|28|warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]|
    main.cpp|31|warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]|
    main.cpp|64|warning: unused parameter ‘s2’ [-Wunused-parameter]|
    main.cpp|64|warning: unused parameter ‘s3’ [-Wunused-parameter]|
    main.cpp|64|warning: unused parameter ‘s4’ [-Wunused-parameter]|
    main.cpp|64|warning: unused parameter ‘s5’ [-Wunused-parameter]|
    Jim

  2. #32
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    void getScore(int& score, ifstream& inFile);
    void calcAverage(int testSc1, int testScr2, int testScr3, int testScr4, int testScr5);
    int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
    int lowest = 100;
    int testScr1, testScr2, testScr3, testScr4, testScr5;
    
    int main()
    {
        ifstream inFile;
        inFile.open("grades.txt");
    
        getScore(testScr1,inFile);
        if (testScr1 = lowest)
        {
         lowest = testScr1;
        }
        getScore(testScr2,inFile);
        if (testScr2 > testScr1)
        {
            lowest = testScr2;
        }
        getScore(testScr3,inFile);
        if (testScr3 > testScr2)
        {
            lowest = testScr3;
        }
        getScore(testScr4,inFile);
        if (testScr4 > testScr3)
        {
            lowest = testScr4;
        }
        getScore(testScr5,inFile);
        if (testScr5 > testScr4)
        {
            lowest = testScr5;
        }
    
            calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5);
    
            inFile.close();
    
        return 0;
    }
    
    void getScore(int& score, ifstream& inFile)
    {
    
        inFile >> score;
    
    
    
    
        void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
        int sum;
        int lowest;
        double average;
    
    
        lowest = findLowest(testScr1, testScr2, testScr3, testScr4, testScr5);
    
        sum = testScr1 + testScr2 + testScr3 + testScr4 + testScr5 - 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 testScr1, int testscr2, int testScr3, int testScr4, int testScr5);
    
    
        int lowest;
        
        cout << "The lowest test score is: " << lowest << endl;
    
        return lowest;
    }
    errors:Error 1 error C2086: 'int lowest' : redefinition c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 76 1 test2
    Error 2 error C2562: 'getScore' : 'void' function returning a value c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 80 1 test2
    3 IntelliSense: return value type does not match the function type c:\Users\JAMES\Documents\Visual Studio 2013\Projects\test2\test2\test2.cpp 80 9 test2


    I am having a hard time understanding what the errors mean.. I have made changes and every time I make changes I get new errors.

  3. #33
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    void getScore(int& score, ifstream& inFile);
    void calcAverage(int testSc1, int testScr2, int testScr3, int testScr4, int testScr5);
    int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
    int lowest = 100;
    int testScr1, testScr2, testScr3, testScr4, testScr5;
    
    int main()
    {
        ifstream inFile;
        inFile.open("grades.txt");
    
        getScore(testScr1, inFile);
        lowest = testScr1;
        getScore(testScr2, inFile);
        if (lowest > testScr2);
        lowest = testScr2;
        getScore(testScr3, inFile);
        if (lowest > testScr3);
        lowest = testScr3;
        getScore(testScr4, inFile);
        if (lowest > testScr4);
        lowest = testScr4;
        getScore(testScr5, inFile);
        if (lowest > testScr5);
        lowest = testScr5;
    
    
            calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5);
    
            inFile.close();
    
        return 0;
    }
    
    void getScore(int& score, ifstream& inFile)
    {
    
        inFile >> score;
    
    
    
    
        void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
        int sum;
        int lowest;
        double average;
    
    
        lowest = findLowest(testScr1, testScr2, testScr3, testScr4, testScr5);
    
        sum = testScr1 + testScr2 + testScr3 + testScr4 + testScr5 - 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 testScr1, int testscr2, int testScr3, int testScr4, int testScr5);
    
    
        int lowest;
        
        cout << "The lowest test score is: " << lowest << endl;
    
        return lowest;
    }
    changed some things again cause I figured out that is not right for if statement..
    this is what I get in return:
    Warning 1 warning C4390: ';' : empty controlled statement found; is this the intent? c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 21 1 test2
    Warning 2 warning C4390: ';' : empty controlled statement found; is this the intent? c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 24 1 test2
    Warning 3 warning C4390: ';' : empty controlled statement found; is this the intent? c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 27 1 test2
    Warning 4 warning C4390: ';' : empty controlled statement found; is this the intent? c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 30 1 test2
    Error 5 error C2086: 'int lowest' : redefinition c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 66 1 test2
    Error 6 error C2562: 'getScore' : 'void' function returning a value c:\users\james\documents\visual studio 2013\projects\test2\test2\test2.cpp 70 1 test2
    7 IntelliSense: return value type does not match the function type c:\Users\JAMES\Documents\Visual Studio 2013\Projects\test2\test2\test2.cpp 70 9 test2

  4. #34
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at each line mentioned in your warning messages, look for "extra" things like semicolons.

    For the first error message, why are you prototyping a function inside your function?

    For the second error read the message carefully, it tells you exactly what is wrong.


    And most importantly stop throwing code at the problem with hopes of it fixing the problem. You need to think about what is the problem before you even sit down at the computer to write the first line of code.


    Jim

  5. #35
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Diablo View Post
    Code:
    if (lowest > testScr2);
    On lines 20, 23, 26, and 29, the semicolon is the problem. Others have tried to tell you this already, and you have not paid attention. There should not be a semicolon after the parentheses of an if statement. The compiler is warning you about it. Other forum members are warning you about it, and you haven't fixed it, after being told several times.

    Your getScore function is declared to return void (nothing), but you are trying to return a value. You cannot do this. Declare it as
    Code:
    int getScore(int& score, ifstream& inFile)
    On line 66, you redeclare the variable lowest. It's already declared on line 51. Delete line 66.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #36
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I can't believe how out of hand this has gotten.

    I think we need to go back to the last program that kind-of sort-of worked.
    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 lowest;
        int testScr1, testScr2, testScr3, testScr4, testScr5;
         
     // I deleted all the semicolons after if() 
     // Semicolons after if() are a bad idea; they make the comparison meaningless because a semicolon is a complete statement that means do nothing.
     // So the if() will do nothing and then run whatever was underneath.
        getScore(testScr1,inFile);
        lowest = testScr1;
        getScore(testScr2,inFile);
        if (lowest > testScr2)
        lowest = testScr2;
        getScore(testScr3,inFile)
        if (lowest > testScr3)
        lowest = testScr3;
        getScore(testScr4,inFile);
        if (lowest > testScr4)
        lowest = testScr4;
        getScore(testScr5,inFile);
        if (lowest > testScr5)
        lowest = 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;
    }
    Now I remember that the problem was that the first number in grades.txt was assumed to be the lowest. But look at the code in main(). You read in a number, assume it is the lowest. Then you read in another number and compare it with the current lowest to see if there is a new, lower number. You keep doing that. That is right. After all the numbers are entered, lowest is correct.

    You don't need to call findLowest() any more. Delete everything to do with that function.

    What you ought to do next is change calcAverage() to receive the lowest number from main(), which is where you calculated it. You do this by passing in an argument. So here you go. I will give you the prototype. You will have to change the definition and the call of calcAverage() yourself.
    Code:
    void calcAverage(int s1, int s2, int s3, int s4, int s5, int lowest);
    This is all I wanted you to do the last time I posted. I'm sorry if I was unclear.
    Last edited by whiteflags; 07-01-2014 at 05:02 PM.

  7. #37
    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 s1, int s2, int s3, int s4, int s5, int lowest);
    int findLowest(int, int, int, int, int);
    int grades(int, int, int, int, int);
    int main()
    {
        ifstream inFile;
        inFile.open("grades.txt");
        int lowest;
        int testScr1, testScr2, testScr3, testScr4, testScr5;
    
        // I deleted all the semicolons after if()
        // Semicolons after if() are a bad idea; they make the comparison meaningless because a semicolon is a complete statement that means do nothing.
        // So the if() will do nothing and then run whatever was underneath.
        getScore(testScr1, inFile);
        lowest = testScr1;
        getScore(testScr2, inFile);
        if (lowest > testScr2)
            lowest = testScr2;
        getScore(testScr3, inFile);
        if (lowest > testScr3)
            lowest = testScr3;
        getScore(testScr4, inFile);
        if (lowest > testScr4)
            lowest = testScr4;
        getScore(testScr5, inFile);
        if (lowest > testScr5)
            lowest = testScr5;
    
        calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5, lowest);
    
        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 lowest)
    {
        int sum;
        
        double average;
    
        
        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;
    }
    OK..had to change a few things.. I did and it currently giving the correct average of the 4 highest score (leaving out the lowest score) but now it does not read out the lowest score at all. I tried to change things a little to fix that only to get back where I was which was reading out the top score wo I went back and this is where I am. I am sure that it is an easy fix but im not seeing it.

  8. #38
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What do you mean it doesn't read the lower score. It reads all five scores, finds and subtracts the lowest score then computes and prints the average of these highest four scores.

    If you mean it is not printing the lowest score, that is because you never actually print that value.

    Jim

  9. #39
    Registered User
    Join Date
    Jun 2014
    Posts
    19
    No it prints the average and its correct so thats good.. I thought I was asking it to print the average at the bottom:
    Code:
    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;
    }

  10. #40
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you mean you want to print the lowest score, you should write a cout statement near the bottom of calcAverage(), or near the bottom of main(). Like I said before, you can delete the findLowest() function as it will never be called.

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