Hey guys.

After the great help i received with my previous thread of reading in a file, i thought id post up my next trouble.

This is my code for my ISBN search function which works fine after the information has been read in to the 'book's.

int BookStore::findISBNRecord()

{
	string key;
	int i = 0;
	bool find = false;
	cout << "Enter the ISBN to search:" << endl;
	cin >> key;		//enter a search key
	cout << endl;

	while (i<5)	//searches all records
	{
		if (key == book[i].isbn) //compares entered key, with the isbn from all books			
		{
			cout << "THIS Record Found" << endl;
			find = true;
			return 0;
		}
		else
			i++;
	}

	if (find == false)
               cout << "Record not found" << endl << endl;
	else
		cout << "Record found" << endl << endl;
		
	return 0;
}

Though i also require another function that will search by Title. What ive done is basically copy the above code, and just change what the temp 'key' is compared to. In this case, it gets compared to book.title obviously.


int BookStore::findTitleRecord()
{
	string key;
	int i = 0;
	bool find = false;
	cout << "Enter the title to search:" << endl;
	cin >> key;		//enter a search key
        cout << endl;

	while (i<5)	//searches all records
	{
		if (key == book[i].title)  //compares entered key, with the title from all books	
		{
			cout << "THIS Record Found" << endl;
			find = true;
			return 0;
		}
		else
			i++;
	}

	if (find == false)
               cout << "Record not found" << endl << endl;
	else
		cout << "Record found" << endl << endl;

	return 0;
}

Though, when i enter in a title, the program goes AWOL into an infinite loop. Could it be because i am entering the string 'key' with spaces? If so, how would i fix it? And/or how else would i enter the string to compare it to the book title?

Oh and also, i used "THIS record found" just so i could keep track of which statement it is getting to.

Thanks for any further assistance! :)

In spite of a slightly strange logic this function can't loop forever if you have 5 or more well initialized book records in the book array.
It's strange that your search function returns 0 in all cases. It's strange that your search function iterate over external array without its real size checking.

This article has been dead for over six months. Start a new discussion instead.