0

Everything in my code made sense to me, but for some reason it ends up only doing the first two cout then exiting the program, with the value of .getHi being the first input value no matter what and .getLow doing the same thing. I'm not good with dynamic allocation or pointers so I figure it might have to do with that, but I can't figure out what is wrong with my code. Any suggestions?

Edit: When I use .getI, it displays the correct values so I think it's somewhere in the class functions that is messing me up.

#include <iostream>
#include <memory>

using namespace std;

void input();

class Scores
{
    public:
        // ConstructorS & destructor
        Scores()
        {
        }
        Scores(int size)
        {
            fPtr = new float[size];
        }
        ~Scores()
        {
            delete [] fPtr;
        }

        //Member functions
        void setI(int x, float y)
        {
            fPtr[x] = y;
        }
        float getI(int x)
        {
            return fPtr[x];
        }
        float getHi()
        {
            int max = fPtr[0];
            for (int i=0;i<size;i++)
            {
                if (fPtr[i] > max)
                {
                    max = fPtr[i];
                }
            }
            return max;
        }
        float getLow()
        {
            int min = fPtr[0];
            for (int i=0;i<size;i++)
            {
                if (fPtr[i] < min)
                {
                    min = fPtr[i];
                }
            }
            return min;
        }
        float getAvg()
        {
            int total = 0;
            for (int i=0;i<size;i++)
            {
                total += fPtr[i];
            }
            return (total/size);
        }
    private:
        // Variables & pointers
        int size;
        float* fPtr;
};

int main()
{
    input();
}

void input()
{
    int nGroups = 0; 
    int nStudents = 0;
    float score;
    cout << "Enter the number of groups: ";
    cin >> nGroups; // Get number of groups
    Scores* arr;
    arr = new Scores[nGroups];
    for (int i=0;i<nGroups;i++) // Do once for each group...
    {
        cout << "Enter the number of students in group " << i+1 << ": "; // Ask for number of students in group
        cin >> nStudents; // Read in number of students
        arr[i] = Scores(nStudents); // Store a new Scores object at each element of an array
        for (int j=0;j<nStudents;j++)
        {
            cout << "Enter the score for student " << j+1 << ": ";
            cin >> score;
            arr[i].setI(j,score);
        }
        cout << "The highest score for group " << i+1 << " was " << arr[i].getHi() << "." << endl;
        cout << "The lowest score for group " << i+1 << " was " << arr[i].getLow() << "." << endl;
        cout << "The average score for group " << i+1 << " was " << arr[i].getAvg() << "." << endl;
    }
    delete [] arr;
}
  • 4
    Your default constructor never sets fPtr to any value, which means that when the destructor runs, it's going to call delete on an undefined/garbage pointer, which will invoke undefined behavior (and likely crash your program). Also you haven't defined any copy constructor or assignment operator -- google "the rule of 3 for C++" for what you need to do about that. – Jeremy Friesner May 13 '17 at 03:48
  • 1
    You should almost never need to use `delete` in C++. Instead, use things like `unique_ptr` and `vector`. – John Zwinck May 13 '17 at 03:53
  • 2
    Possible duplicate of [What is The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Miles Budnek May 13 '17 at 04:04

1 Answers1

0

Check you Score(int) constructor. You use the size argument but don't initialize the size member, leaving it in an uninitialized state.

Your code:

Scores(int size)
{
    fPtr = new float[size];
}

You need to do:

Scores(int size) : size(size)
{
    fPtr = new float[size];
}

After this you can use your class variable size inside of getHi, getLow and getAvg.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Jorge Omar Medra
  • 978
  • 1
  • 9
  • 19