Segmentation fault (core dump) error?

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?

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <string>

using namespace 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
   
}
     
if ((det+u)->score > (det+u+1)->score)

det+u+1 points off the end of the array
Topic archived. No new replies allowed.