Objects in C++ help

Hi, I have an assignment, that i'm rather confused on how to execute and could use the community's help on figuring this one out.

We did most of this in class, but we have to add to it the ability to find the low and high score in the object and to output it within the main.
My code as it is now, outputs my low score as -1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include <iostream>
using namespace std;

class Averager {
private:
    int sum;
    int count;
public:
    Averager() { sum = 0; count = 0; }
    void tallyScore(int score) { sum += score; count++; }
    double getAverage() { return 1.0 * sum / count; }
    int getCount() { return count; }
    int lowscore(int score1) { return score1=score1; }
    int highscore(int score2) { return score2=score2; }
};

int main() {
    int score; // Score, entered by the user
    Averager avg; // For computing average of scores
    Averager avg_a; // For computing average of the A's
    
    cout << "Enter test score (-1 when finished): ";
    cin >> score;
    while (score != -1) {
        avg.tallyScore(score);
        if (score >= 90)
            avg_a.tallyScore(score);
        if(score!=-1 && score<avg.lowscore(score))
            avg.lowscore(score);
        cout << "Enter test score (-1 when finished): ";
        cin >> score;
    }
    
    if (avg.getCount() > 0)
        cout << "The average is: " << avg.getAverage() << endl;
    if (avg_a.getCount() > 0)
        cout << "The average of A's: " << avg_a.getAverage() << endl;
    
        cout << "The lowest score was: " << avg.lowscore(score) << endl;
    
    
    system("pause");
    return 0;
}
Your lowscore does nothing aside from returning passed argument.
As after while loop score variable will be equal to -1, it outputs it on line 39
MiiNiPaa is correct. Also, to find the lowest or highest score, you will need to keep track of them. When you call highscore(...) or lowscore(...), you're just returning whatever was passed to it. I took the liberty of altering your code. This should work just fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Averager
{
    static constexpr unsigned int MAX_STUDENTS = 30;

    double scoreSum;
    vector <unsigned int> scores;
public:
    Averager():scoreSum(0){scores.reserve(MAX_STUDENTS);}

    void tallyScore(const unsigned int& scoreToTally){scoreSum += scoreToTally; scores.push_back(scoreToTally);}
    void sortContainer() {sort(scores.begin(), scores.end());}

    const unsigned int getScoreCount() const {return scores.size();}
    const double getAVG() const {return scoreSum / scores.size();}
    const unsigned int getHighestScore() const {return scores.at(scores.size() - 1);}
    const unsigned int getLowestScore() const {return scores.at(0);}
};

int main()
{
    int scoreInput;
    Averager totalScores, allStars;

    cout << "Enter a student's score (-1 when finished):\n-----\nGrade 1: ";

    while(cin >> scoreInput)
    {
        static unsigned int scoreCounter = 2;

        if(scoreInput < 0)
            break;
        else if(scoreInput >= 90)
            allStars.tallyScore(scoreInput);

        totalScores.tallyScore(scoreInput);

        cout << "Grade " << scoreCounter++ << ": ";
    }

    totalScores.sortContainer();
    allStars.sortContainer();

    if(totalScores.getScoreCount() > 0)
    {
        cout << "\nAVG of All Scores: " << totalScores.getAVG() << endl;

        if(allStars.getScoreCount() > 0)
            cout << "AVG of A's: " << allStars.getAVG() << endl;

        cout << "Highest Score: " << totalScores.getHighestScore() << endl;
        cout << "Lowest Score: " << totalScores.getLowestScore() << endl;
    }
    else
        cout << "\nInput at least one score to see data\n";

    return 0;
}
Last edited on
> vector <unsigned int> scores;
memory waste.

> void sortContainer() {sort(scores.begin(), scores.end());}
given that you only care about the max/min elements, that's quite overkill.

Also, error prone, as a call to `get{Highest,Lowest}Score()' requires a previous call to `sortContainer()'
I havent learned in class anything about vectors or algorithms. So without completely changing the code (except the obvious call mistakes i made) and still using iostream or iomanip is there another way to go about this???
Add member variables to Averager to store min and max values. Add code to tallyScore to manage those min and max variables. Make lowscore and highscore to not take any arguments and return current min/max value.
Here's another way to do it. Step back and think about how the code computes the average. It keeps track of the number of scores and the total. tallyScore() updates these values and getAverage() uses them to return the average.

You can keep track of the high and low scores the same way:
- declare variables high and low.
- modify tallyScore() to update the high and low variables.
- add getHigh() and getLow() methods to return the high and low variables.
- modify the constructor to initialize high and low.

What should you initialize high and low to? Set high to an absurdly LOW number and set low to an absurdly HIGH number. That way the first time you call tallyScore(), they will both be updated. Ideally, you will initialize high and low to the lowest and highest possible values. These are defined in numeric_limits: http://www.cplusplus.com/reference/limits/numeric_limits/
MiiNiPaa and dhayden, your replies were clear and concise and helped me so much! Thank you!!
Topic archived. No new replies allowed.