Hello, I am having troubles getting program to properly compare and sort a list of strings.
I don't have any errors so I believe it is something that makes the program think a true = false, or something like that. I am pretty new to debugging.
void sortByAuthor(Book books[], int count) // Here is where I have the problem.
{
int alphaIndex;
string temp;
for(int i = 0; i < count; i++){ // library
for(int h = i + 1; h < count; h++){ // unsorted list
if(books[h].author < books[alphaIndex].author)
alphaIndex = h; // Swap
}
temp = books[alphaIndex].author;
books[alphaIndex] = books[i];
books[i].author = temp;
}
}
int showBooksByTitle (Book books[], int count, string title) // I don't think my problem is
// here but this section may be useful.
{
int titleCount = 0;
int match;
for(int i = 0; i < 1000; i++)
{
match = books[i].title.find(title);
if(match!= string::npos)
{
cout << endl << books[i].title << " (" << books[i].author << ")" << endl;
titleCount++;
}
}
cout << endl << titleCount << " records found." << endl;
return 0;
}
case'T': // Sample of cutput
cout << endl << "Please enter all or part of the title: ";
cin >> title;
showBooksByTitle(books, count, title);
sortByTitle(books, count);
break;
intalphaIndex; // alphaIndex is not initialised, it contains garbage
string temp;
for(int i = 0; i < count; i++){ // library
for(int h = i + 1; h < count; h++){ // unsorted list
if(books[h].author < books[alphaIndex].author) // alphaIndex still contains garbage
Almost certainly you have an out of bounds array access, causing the program to crash.