Trouble with While loop

using a string as an exit condition for the while loop

Page 1 of 1

1 Replies - 1347 Views - Last Post: 01 March 2009 - 06:31 PM Rate Topic: -----

#1 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Trouble with While loop

Posted 01 March 2009 - 06:09 PM

Hello,

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with While loop

#2 OrganizedChaos  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: Trouble with While loop

Posted 01 March 2009 - 06:31 PM

I don't even think that if statement you mentioned is necessary...
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
		found = false; //If you're searching for multiple books and you find a book the first time around, found will stay at true and then you'll have an issue if you're searching for a book that isn't there.
		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(found != true) 
		{
		cout << "Book code not found. Re-enter book code or enter exit " << "to return to search menu" << endl;
		cin >> line;
		}  //end if

	}  //end "exit" while, user typed exit.
}



The way you had it was the user could only retry the book code if he/she had typed "exit" to begin with...
Also, I added something at the beginning of the first while loop.

Hope this does something to help. Not sure if it will fix the problem n_n;;

This post has been edited by OrganizedChaos: 01 March 2009 - 06:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1