Issue with functions searching a database

I am trying to write a program to search a library file with the name of a book or author and return the books that match the searched string in some way. For instance, if I search "Develop" it should display Game Development Essentials(Novak) and Developing Games in Java(Brackeen) and tell me that 2 records were found. Currently, it shows all the records regardless of what i search for, even if it is jibberish. Am I missing something in my functions? should I include the code that accesses these functions? Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//If the user chooses A or a
int showBooksByAuthor (int count, string name)
{
	char choice;
	int index = 0;

	//Searches each string slot in the author array for a match to the user-inputed string
	for(int i = 0; i < count; i++)
	{
		if(bookAuthor[i].find(name))
		{
			//Displays the matching array slot and increases the records found by 1
			cout << bookTitle[i] << "(" << bookAuthor[i] << ")" << endl;
			index++;
		}
	}
	index++;

	cout << index << " record(s) found" << endl;
	cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
	cin >> choice;
	return choice;
}

//If the user chooses T or t
int showBooksByTitle (int count, string title)
{
	char choice;
	int index = 0;

	//Searches each string slot in the title array for a match to the user-inputed string
	for(int i = 0; i < count; i++)
	{
		if(bookTitle[i].find(title))
		{
			//Displays the matching array slot and increases the records found by 1
			cout << bookTitle[i] << "(" << bookTitle[i] << ")" << endl;
			index++;
		}
	}
	index++;

	cout << index << " record(s) found" << endl;

	cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
	cin >> choice;
	return choice;
}
Lines 10 and 34: your condition is true for every string::find() except those where the substring you are searching for is at the very beginning of the string.

They should read: if (bookTitle[i].find(title)!=string::npos)

This is in the documentation, which takes some practice reading to not miss things like this.

(Alas.)


The next thing to notice is that you are mixing program flow control and user interaction into your functions. Don't do that.

All that interacting with the user stuff should be in its own function(s), like int mainMenu() or void showMainMenu() and int getUserInput().


You will notice that your program search is nice and general, but it is unfortunately hampered by character case. You can do some case-insensitive (more or less) find functions using the STL.

1
2
3
4
5
#include <algorithm>
#include <cctype>
#include <functional>
#include <iterator>
#include <string> 
1
2
3
4
5
6
7
8
template <typename CharT>
struct ci_compare: std::binary_function <CharT, CharT, bool>
  {
  bool operator () ( CharT a, CharT b ) const
    {
    return std::tolower( a ) == std::tolower( b );
    }
  };
1
2
3
4
5
6
7
8
bool is_substring_ci( const std::string& s, const std::string& substring )
  {
  return std::search(
           s.begin(), s.end(),
           substring.begin(), substring.end(),
           ci_compare <char> ()
           ) != s.end();
  }
34
35
36
		if(is_substring_ci(bookTitle[i],title))
		{
			//Displays... 

Hope this helps.
Topic archived. No new replies allowed.