If geeks love it, we’re on it

Howdy, Stranger!

You found the friendliest gaming & tech geeks around. Say hello!

Help searching for specific words within strings in arrays in C++

I am creating a library database where I read the authors and titles in from a file and store the info in parallel arrays. Then, I am trying to search for specific pieces within that array. For instance, I am reading in the author "Ernest Hemingway" in and storing it in a spot in the author array.

Then I have to be able to search for "Hemingway" and have it pop up with his book that is in the parallel array. My problem is, even if I search for "Ernest", this search won't return "Ernest Hemingway".

Now, part of my code to read the titles and authors is such:
string bookTitle[1000];
string bookAuthor[1000];

//for loop that will save titles and authors in array
for (int i = 0; i < 1000; i++) 
{
    //while there are things left to get in file
    if(inFile)
    {
       getline(inFile, bookTitle[i]); 
       getline(inFile, bookAuthor[i]);
            
       //keep count of number of books
       count++;         
    }
}

And part of my code to search for the authors is:
string name;
cout << "Author's name: ";
getline(cin, name);
...
int loc = 0; //declare for loop variable

//while loop will continue through array until end of library
while (loc < count)
{
    if (bookAuthor[loc] == name)
    {
       cout << bookTitle[loc] << " (" << bookAuthor[loc] << ")"
            << endl
    }
    loc++;
}

I have a feeling that my problem is that I'm saving the titles and authors as strings. I think that strings are saved a certain way so you can't search within it. Thus, the name and bookAuthor[loc] will never be equal. What should I do though? What is possible way of coding up a solution to my problem?

Thanks for the help!!

Comments

Sign In or Register to comment.