Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gpalin

Finding a string inside a string

This topic is 5575 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on a database program (in C++), which (so far) contains strings to represent names of songs in the database. I'm trying to include a method to search for songs with a user-input keyword. Here's the code I have for this particular method:
void Database::findSong(string keyword) {
    cout << endl;

    map< string, vector<Song> >::iterator artistIt = artists.begin();
    int i = 1;
    while (artistIt != artists.end()) {
        vector< Song >::iterator songIt = artists[artistIt->first].begin();
        while (songIt != artists[artistIt->first].end()) {
            if ((songIt->getTitle()).find(keyword)) { //this is the line in question

                cout << i << ". " << songIt->getTitle() << " (" << songIt->getArtist() << ")" << endl;
            }
            songIt++;
            i++;
        }
        artistIt++;
    }
}
I get no errors when I compile, and it runs without trouble, but the method dplays the whole database - not just the songs I'm looking for. Meaning, if I input a word common to a few songs (but not all of them), the program displays them all anyway. Does the code above need changing somehow? I've looked at it and looked at it, and it seems to me that it should work. Any suggestions? I've marked the line that supposedly does the searching. Grant Palin [edited by - gpalin on June 16, 2003 6:59:35 PM] [edited by - gpalin on June 16, 2003 7:01:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
Perhaps you should look at what find() returns if it DOESN''T find the string.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
BeerHunter, thanks. That change worked just fine.

And Sneftel, thanks. I looked it up, and found out what it did. Thanks for the push there.

Grant Palin

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!