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;
}