help with 'string.find'

This is a discussion on help with 'string.find' within the C++ Programming forums, part of the General Programming Boards category; Code: location = temp_string.find(search_string); if(location > -1) { cout << "found!" << endl; } else { cout << "not found!" ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    84

    help with 'string.find'

    Code:
    	location = temp_string.find(search_string);
    		if(location > -1)
    		{
    			cout << "found!" << endl;
    		}
    		else
    		{
    			cout << "not found!" << endl;
    		}
    hi,

    in this code, i'm searching for the search_string in the temp_string.

    when the search_string is one word such as "text" and in "text" is in temp_string at location 1 or greater, then my code says found!. however, i have a line that starts out with "text" so its in location 0. my code says "not found!"

    in other words, if i have a word at location 0, it doesn't return 0 and if i have the word at location 1 or higher, then it does return that number into location.

    i don't think its supposed to happen like that. in the past i found an error in getline thank to y'all. i'm using msvc++6 with sp6.

    any suggestions?

    thanks,
    barneygumble742

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Do you have a more complete example? Data types are important.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    One would say use std::string::npos to determine if you found it.
    Code:
    if(loc == std::string::npos)
    {
        //not found
    }
    else
    {
       //found
    }
    Woop?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    Quote Originally Posted by Dave_Sinkula
    Do you have a more complete example? Data types are important.
    Code:
    string one = "Born in Boston, Massachusetts, to a tallow-maker, Franklin became a newspaper editor.";
    
    string two = "Born";
    
    one.find(two);         // returns -1
    
    string one = " Born in Boston, Massachusetts, to a tallow-maker, Franklin became a newspaper editor.";
    
    string two = "Born";
    
    one.find(two);         // returns 0
    the only difference between the two large strings is the space in front in the second assertion. i'm sure that find should work even without the space.

    prog-bman, i tried the npos way and still nothing.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
       {
          string one = "Born in Boston...";
          string two = "Born";
          cout << one.find(two) << '\n';         // returns 0
       }
       {
          string one = " Born in Boston...";
          string two = "Born";
          cout << one.find(two) << '\n';         // returns 1
       }
       {
          string one = "  Born in Boston...";
          string two = "Born";
          cout << one.find(two) << '\n';         // returns 2
       }
       {
          string one = " in Boston...";
          string two = "Born";
          cout << one.find(two) << '\n';         // returns npos
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    thanks dave. i know thats how its supposed to work. that's why my original post had my compiler and the previous error i had with getline. i'm sure there are much more problems with msvc++ with sp6.

    what's your compiler? and how can i fix this?

    thanks,
    mark

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by barneygumble742
    what's your compiler?
    MSVC6 and BC5.5.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    i tried my code in a unix machine using g++ and i'm still getting the same error.

    however my code is assigning text to an array of strings.

    like:

    Code:
    	description[3][2][6] = "Born in Boston, Massachusetts, to a tallow-maker, Franklin became a newspaper editor.";
    in the above case, it won't find Born however in the bottom case, it will find it.

    Code:
    	description[3][2][6] = " Born in Boston, Massachusetts, to a tallow-maker, Franklin became a newspaper editor.";
    again, the only difference is the space. i tried prog-bman's code and dave's code which works in msvc++ and unix. and even my code works when i'm not using the arrays. is there something i'm missing when it comes to strings and arrays?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    description[3][2][6] = "...";
    What are you trying to do here? That's a 3 dimensional array which has already been declared.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    i'm already traversing the 3d array however while i'm traversing, i want to search the text. and if the particular string is found, then i'm printing the text. its a primitive search/database i guess.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    i'm very sorry for wasting everyone's time.

    the find function works in the muti-dimensional array of strings and the regular string the same exact way.

    the problem was at my end because the search parameter i had always had a space before the word. so in reality, my search parameter was " Born" rather than "Born". in fact, i accidently came across that realization.

    thanks everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string.find()
    By franse in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2008, 04:19 PM
  2. Using while with string.find()?
    By suzakugaiden in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2006, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21