Thread: Help me with string "find(); function plz

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    Help me with string "find(); function plz

    in the string.find() func it teturns garbage value if the text is not found..
    is this possible that it returns 1 if the text found and 0 if it fails

    like in this example
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    int main ()
    {
       string a="saqib.txt";
       cout << a.find("xts");
       return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by fredsilvester93
    in the string.find() func it teturns garbage value if the text is not found..
    No, it returns std::string::npos if the search string is not found.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    yes i knw that! is there any way that of the text is not found, it returns 0 instead of garbage

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by fredsilvester93 View Post
    yes i knw that! is there any way that of the text is not found, it returns 0 instead of garbage
    Write such a function if you want that exact behaviour .

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Why would you want to return 0? That's not a good idea as the function returns the position in the source string where it finds the text in question. Since a return value of 0 is a valid location to find your text (if you looked for "saq" for example), it seems a poor choice to also return a value of 0 when the function fails to find a value. Does a return value of 0 represent both a possible success and a failure state? Seems to be something you'd rather avoid happening where possible.

    If you really want that behavior, you could write a wrapper function for that functionality. If the find call returns std::string::npos you could return a 0 from your function.

    BTW, it's not garbage if the specified return result is a well-defined value. It returns what it returns.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    i want to chck if the extension '.txt' exists, then perform funtion on file, but i cant do it, because it is returning garbage value, what chck condition should i use??

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Like we said, the function returns std::string::npos if the text is not found, use it:
    Code:
    std::string a = "saqib.txt";
    if( a.find("xts") != std::string::npos )
    {
      // xts was found
    }
    else
    {
      // xts was not found
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function to find and replace a part of a string
    By 9988776655 in forum C Programming
    Replies: 2
    Last Post: 02-02-2008, 01:39 AM
  2. Replies: 11
    Last Post: 12-11-2005, 11:50 PM
  3. String member function find error
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2005, 06:51 PM
  4. find string function
    By barneygumble742 in forum C++ Programming
    Replies: 1
    Last Post: 07-18-2005, 04:18 PM
  5. can't find error function (s/a "erf") in borland c++ libraries
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2001, 12:04 PM