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

  • mertesnmertesn I am Bobby Miller Yukon, OK Icrontian
    edited Aug 2011
    Using strings isn't a problem. I believe you're looking for the 'find' method that is part of the string class.

    That should get you started.
  • Straight_ManStraight_Man Geeky, in my own way Punta Gorda, FL Icrontian
    edited Aug 2011
    Hemingway with quotes used to give only strings of text type that contained Hemingway only. Google is a combo of extreme c++ development and a huge set of blade stacks. Feed it Hemingway and you get thousands of results because it searches multiply. With the latest c++ I don't know. [url]http://www.http://discuss.extremetech.com/forums/default.aspx/[/url] had an area for programmers and might still have.
  • pragtasticpragtastic Alexandria, VA Icrontian
    edited Aug 2011
    I don't even.
    TiberiusLazarus
Sign In or Register to comment.