My program description is to create a program that takes names and scores as pairs and uses pointers and a dynamic array to store them. I am to then take those scores and pass them to a function that sorts them in ascending order and another function that gets the average. This is actually the second half of a problem and I've already gotten the sort and average functions to work correctly, I just had to implement a struct for the second half where it got tricky. I read that my error has something to do with allocation of memory with pointers, but I honestly have no idea what the problem would look like.. Any ideas what I messed up on?
#include <iostream>
#include <string>
usingnamespace std;
struct details//struct to store both name and score
{
string name;//used to store the name of the student
int score;//used to store the score of the student
};
void sortAscend(details arraySort[],int sortSize)
{
details *det = arraySort;
int u;
details arraySwitch;//Variable to keep track of counts in the array
bool sortCheck;//Checks to see if the sort is finished or not
sortCheck = false;//Starts off at false
do
{
sortCheck = false;
//keeps it false through each iteration and will trigger an exit
for(u = 1; u < sortSize; u++)
{
if ((det+u)->score > (det+u+1)->score)
{
arraySwitch = *(det+u);
*(det+u) = *(det+u+1);
*(det+u+1) = arraySwitch;
sortCheck = true;//causes the loop to run again
}
}
}
while(sortCheck);
}
int arrayAvg(details scoreList[],int scoreSize)
{
details *scorePtr = scoreList;
int sum = 0;//will be used to sum together the scores
int x;//Used to keep count in for loop
int average;//will hold the average
for(x = 0; x < scoreSize; x++)
{
sum = sum + (scorePtr + x)->score;
}
average = sum / scoreSize;//divides sum by how many scores there are
return average;//returns the value of average that is then output
}
int main()
{
struct details *array,*t;//used to pass scores and names
array = NULL;//Creates the integer that will later be an array
int size;//Will store in the size of the array
int loopCount;//Will keep count in the for loop
int sortOut;//Used in the for loop for outputting the sorted array
cout << "Please enter how many scores will be input: ";
cin >> size;//Stores size of array
array = new details[size];//Creates dynamic array
t = array;
for(loopCount = 0; loopCount < size; loopCount++)//input loop for array
{
cout << "\n"<<"Please enter the name and score seperated by spaces: ";
cin >> (t + loopCount)->name;//sets the name at this position
cin >> (t + loopCount)->score;//sets the score at this position
if((t + loopCount)->score < 0)
{
cout << "You input a negative score, program ending" << endl;
return 0;
}
else
{
}
}
sortAscend(t,size);//sends array to the ascending order sort function
cout << "The scores sorted in ascending order are: ";
for(sortOut = 0; sortOut < size; sortOut++)//outputs the sorted array
{
cout << (t + sortOut)->score << " ";
}
cout << endl;
cout << "The average of the scores is: " << arrayAvg(t,size) << endl;
delete [] array;//deallocates the dynamic array
array = NULL;//Makes sure that array is not used again
return 0;//ends program
}