Can anyone explain to me why my function is not working...?

This is a discussion on Can anyone explain to me why my function is not working...? within the C++ Programming forums, part of the General Programming Boards category; EDIT: Nevermind... Realized my mistake. You can delete this....

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Can anyone explain to me why my function is not working...?

    EDIT: Nevermind...
    Realized my mistake.

    You can delete this.
    Last edited by Programmer_P; 06-02-2010 at 01:36 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Instead of editing out, post your solution next time
    Help people out by giving a solution to said problem if they have the same.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Well, its not really all that important.
    Turns out the function wasn't returning the wrong values at all, like I thought it was.

    I just forgot to output the right messages...
    I meant to do this:
    Code:
    if (containsString) {
       cout << '\"' << sourceString << " contains \"" << searchString << '\"' <<end;
    }
    
    else {
       cout << '\"' << sourceString << " does NOT contain \"" << searchString << '\"' <<end;
    }
    But instead, I ended up doing:
    Code:
    if (containsString) {
       cout << '\"' << sourceString << " contains \"" << searchString << '\"' <<end;
    }
    
    else {
       cout << '\"' << sourceString << " contains \"" << searchString << '\"' <<end;
    }
    by accident, thus outputting the same message at either statement. But, in case anyone wants to view the now-working code, here is the link:

    C++ code - 279 lines - codepad
    Last edited by Programmer_P; 06-02-2010 at 01:53 PM.

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Hmm...on second thought, looks like the "if (searchStringSize < sourceStringSize)" block is returning the wrong value from the containsString() function if sourceString doesn't contain searchString. It will return true regardless...

    http://codepad.org/njKA37LE

    I'll have to focus on that part of the code and fix whatever problem is there in my logic.
    Last edited by Programmer_P; 06-02-2010 at 01:55 PM.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    What am I missing in my logic?
    Code:
    int currentIndexOfSearchString = 0;
            for (int i = 0; i < sourceStringSize; i++) {
                 if (sourceString[i] == searchString[0]) { //then we have now found the first character of the searchString in the sourceString
                     for (int i2 = i; i2 < sourceStringSize && i2 < searchStringSize; i2++) {
                          if (sourceString[i2] != searchString[currentIndexOfSearchString]) {
                              if (i2 == searchStringSize - 1) { //we're at the last character of the searchString
                                  return false;
                              }
                                 
                              else {
                                  if (i2 < sourceStringSize - searchStringSize) { //meaning we have enough characters in sourceString to store a possible match of searchString
                                      i = i2; //set the outer for loop iterator to the value of the inner one
                                      break; //out of this loop at the first char of the sourceString that doesn't match the current char of the searchString
                                  }
                                  else {
                                      return false;
                                  }
                              }               
                          }
                          else {
                              //we're good so far...
                          }
                          currentIndexOfSearchString++; //increment to the next character index in the searchString
                     }
     
                 }  
            }
    Obviously I need another "return false" line in there somewhere, but I have no idea where to put it.

    Current source code:
    C++ code - 284 lines - codepad

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Are you trying to write a version of strstr() for practice or such? If not, there are functions you can use to make your life simpler if you are using the string class.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by syzygy View Post
    Are you trying to write a version of strstr() for practice or such? If not, there are functions you can use to make your life simpler if you are using the string class.
    Functions such as what?
    I don't know of any function in the string class for checking to see if one string contains another string (there is string::find(), but that returns the position of the string in the string, and I just want to know if one string contains another string). That's why I'm writing my own.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Like I mentioned, strstr() will find a string within another string.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    there is string::find(), but that returns the position of the string in the string, and I just want to know if one string contains another string
    O_o

    If you know this, and this isn't a learning exercise, why on earth aren't you using `std::string::find' to implement this?

    Soma

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by syzygy View Post
    Like I mentioned, strstr() will find a string within another string.
    And, like I mentioned, I need a function which returns a bool value to indicate whether or not one string contains another string.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Why a bool? Can you not convert the logic of "if this is null, it's false, otherwise it's true" yourself?

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Thumbs up

    Quote Originally Posted by phantomotap View Post
    O_o

    If you know this, and this isn't a learning exercise, why on earth aren't you using `std::string::find' to implement this?

    Soma
    Good question.
    And the answer is....
    Because I didn't think of it.

    Thanks for that though. It works now.
    And the working version can be found at:
    C++ code - 265 lines - codepad

    EDIT: And for the record, this is a learning exercise...
    I'm writing a program to convert enum values to strings as both a learning exercise, and because I need a program to do such a thing for another program I'm currently working on.
    Last edited by Programmer_P; 06-02-2010 at 05:29 PM.

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by syzygy View Post
    Why a bool? Can you not convert the logic of "if this is null, it's false, otherwise it's true" yourself?
    Yes I can, but its easier using bool values.

  14. #14
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    So this:
    Code:
    void somefunc()
    {
        bool containsString = str.find("awesome") != std::string::npos;
    }
    is not as good as this:
    Code:
    bool ContainsString(string x, string y)
    {
        //Insert bunch of custom code here
    
        return contains;
    }
    void somefunc()
    {
        bool containsString = ContainsString(str, "awesome");
    }
    Woop?

  15. #15
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by prog-bman View Post
    So this:
    Code:
    void somefunc()
    {
        bool containsString = str.find("awesome") != std::string::npos;
    }
    is not as good as this:
    Code:
    bool ContainsString(string x, string y)
    {
        //Insert bunch of custom code here
    
        return contains;
    }
    void somefunc()
    {
        bool containsString = ContainsString(str, "awesome");
    }
    Hahaha...yeah, I see your point. But what would be the fun in that...?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sudoku Brute Force Algorithm Help (recursion?)
    By ridilla in forum C Programming
    Replies: 22
    Last Post: 05-07-2010, 03:31 PM
  2. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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