Like Tree1Likes
  • 1 Post By hk_mp5kpdw

Help me with string "find(); function plz

This is a discussion on Help me with string "find(); function plz within the C++ Programming forums, part of the General Programming Boards category; in the string.find() func it teturns garbage value if the text is not found.. is this possible that it returns ...

  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
    22,269
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,526
    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 .
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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,806
    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
    }
    Elysia likes this.
    "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, 05: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

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