I have a function in a class implementation file (of a class I created Book.h), that searches the private data of the class (linked list).
The user is supposed to enter a book code to search the shelf of books (my linked list), and if the function found one, it should display the information.
My problem is, I'm having trouble exiting this loop, when I type in "exit" to get out of the while loop.
/*
This function is: bookSearch()
User searches book store for a matching book code, and this will display
the results found in the linked list.
Use a temporary pointer to traverse the linked list.
Function prints out the result/s found into columns.
If no match is found, display:
Book code not found. Re-enter book code or enter exit to return to
search menu.
Loops until exit flag is entered by user.
PRECONDITIONS:
User must have typed 'b' or 'B' to enter this search book code function
otherwise an error was displayed.
POSTCONDITIONS:
Must type "exit" or "EXIT" to exit this search function.
*/
void Book::searchCode() { //searches the link list for book code data
string line = " "; //string to enter book code characters into.
bool found = false;
//my trouble line here
while(line != "exit" && line != "EXIT") { //exit flag is used to exit
cout << "Enter book code to search for (or type exit to return "
<< "to search menu): " << endl;
cin >> line; //get input from user until return is hit.
//search the linked list for the book code entered.
bookInfo* temp; // created pointer to traverse list.
temp = Head; //start at beginning of list.
while(temp != NULL) { //goes until temp = last value of last node.
//compare book codes, to the one supplied by user
if(temp->code == line) { //if we have found a match
cout << "Book code" << setw (16) << temp->code << endl;
cout << "Title" << setw(16) << temp->title << endl;
cout << "Publisher code" << setw(16) << temp->publisherCode << endl;
cout << "Type" << setw(16) << temp->type << endl;
cout << fixed << setprecision(2); //iomanip include, formats the
//variable to dispaly in 1/100ths of a dollar.
cout << "Price" << setw(16) << temp->price << endl;
cout << "Paperback" << setw(16) << temp->paperback << endl;
//if the if statement is executed, set the bool found = true
found = true;
} //end if
temp = temp->Next; //go to next node
} //end while, done traversing linked list
if(line != "exit" && line != "EXIT") { //if they typed conditions
//skip the error message below
//nested if, only show if user didn't want to get out of loop
if(found != true) {
cout << "Book code not found. Re-enter book code or enter exit "
<< "to return to search menu" << endl;
cin >> line;
} //end nested if
} //end if "exit"
} //end "exit" while, user typed exit.
}
I have tried both the operators || && for the conditions of the while loop, and also put parenthesis around each of the (line != "exit").
The funny part is, the if statement that has the exact same conditions, actually works.

New Topic/Question
Reply




MultiQuote




|