Hello Everyone,
I'll just jump right into it.
Code:
void searchLibrary(book *list)
{
string key;
string search;
book *searchList = NULL;
book *temp = NULL;
book *found = NULL;
int ctr = 0;
int i = 0;
int resp = 0;
screenHeader("Search Function");
cout << " Type out one of the options below." << endl << endl << endl << endl;
cout << "\tHow would you like to search entries?" << endl << endl;
cout << "\t\t- Author" << endl << endl;
cout << "\t\t- Title" << endl << endl;
cout << "\tSearch entires by: ";
getline(cin, search);
if(search.at(0) == 'a' || search.at(0) == 'A'){
//search by author
cout << endl << "\tEnter the author: ";
getline(cin, key); // Key to look for when searching
while(list){ // Search the whole array
if(key == list->author){ // If a match
if(ctr == 0){
found = new book;
found = list;
searchList = found;
found->next = NULL;
ctr++; // Counter for the number of results found
}
else{
temp = new book;
temp = list;
temp->next = NULL;
found->next = temp;
found = found->next;
}
}
list = list->next;
}
if(ctr == 0) // If the counter equals zero, then no results were found
cout << endl << endl << " ** No results were found **" << endl << endl << endl;
else{ // Display the results
system("cls");
screenHeader("Search Results");
cout << "\tResults Found: " << ctr << endl << endl;
tableDisplay(searchList);
}
transitionScreen();
}
else if(search.at(0) == 't' || search.at(0) == 'T'){
//search by title
cout << endl << "\tEnter the title: ";
getline(cin, key);
while(list){ // Search the list
if(key == list->title){ // If there is a match
if(ctr == 0){
found = new book;
found = list;
searchList = found;
found->next = NULL;
ctr++; // Counter for the number of results found
}
else{
temp = new book;
temp = list;
temp->next = NULL;
found->next = temp;
found = found->next;
}
}
list = list->next;
}
if(ctr == 0) // If the counter equals zero, then no results were found
cout << " ** No results were found **" << endl << endl << endl;
else{ // Display the results
system("cls");
screenHeader("Search Results");
cout << "\tResults Found: " << ctr << endl << endl;
tableDisplay(searchList);
}
transitionScreen();
}
else{
cout << "You did not enter one of the following options." << endl << endl << endl;
transitionScreen();
}
}
Code:
searchLibrary(orderList);
orderList is the head of a list I'm passing to the search function. The problem is in the search function, when found->next = NULL executes, list->next also updates to equal NULL, which means I can no longer continue to search list.
Thanks for your help!