Several of you helped me with this project a few days back. I got the code to compile thanks to your help, but now I'm running into a segfault. I've tracked down what code is causing it, but I have no idea how to fix it!
I've created an array of structs at global scope, (bad, I know. The instructor specifically said do it, or i'd try for a better solution,) and it works fine in int main() and the other functions where I'm using it. However, I've created a func to sort the array, and the function is causing a segfault. Upon further investigation, the first instance of accessing the array (whatever that happens to be) is causing the problem.
Edit: By way of clarification, I can read and write listCD[] without problems everywhere else in the code without a problem. It's only when I do this in listSort() that it causes a problem.
#define listSizeMax 100
struct albumDatabase {
string title;
string band;
} listCD[listSizeMax];
...
void listSort(albumDatabase[listSizeMax],int,char);
int main()
{
int i, listSize; // For the purpose of shortening this code fragment I'm not
// showing where I set listSize, but it initializes correctly.
char selection,sortPrimary;
...
switch(sortPrimary)
{
case't':
case'T':
cout << "Sorting database by song title...";
listSort(&listCD[listSizeMax],listSize,'t');
break;
case'b':
case'B':
cout << "Sorting database by song title...";
listSort(&listCD[listSizeMax],listSize,'b');
break;
}
...
}
void listSort(albumDatabase listCD[listSizeMax], int length, char sortPrimary)
{
int indexSwap, index;
string temp[2];
cout << "\ndebug point #1" << endl; cout.flush();
for (index = 1; index < length; index++)
{
cout << "\ndebug point #1.5" << endl; cout.flush();
cout << listCD[0].title << endl; cout.flush();
cout << "\ndebug point #2" << endl; cout.flush();
... // This block contains an insertion sort algorithm. The next statement is
// an if statement with a monstrous test condition. I inserted the above
// three lines simply for debug. Execution core dumps after point 1.5,
// and never reaches point 2.
}
...
}