CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 2012
    Posts
    18

    String.Find() not working ...

    Hi guys, sorry - I posted this in another thread but realized it should be in it's own...

    Here's what's up.
    I have a string that I have set up like this:

    std:string comData;
    and comData contains this: "X:0.59Y:0.42Z:-171.80"

    Now, I need to know the positions of X: , Y: and Z: in that string so I tried using find() like this:

    Code:
    size_t xpos, ypos, zpos;  
    
    xpos = comData.find("X:");
    ypos = comData.find("Y:");
    zpos = comData.find("Z:");
    Which I would expect to return: 0, 6, 12
    But C++ has stumped me again - the variables xpos, ypos and zpos are empty

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: String.Find() not working ...

    Quote Originally Posted by ChadReitsma View Post
    Code:
    size_t xpos, ypos, zpos;  
    
    xpos = comData.find("X:");
    ypos = comData.find("Y:");
    zpos = comData.find("Z:");
    ...
    But C++ has stumped me again - the variables xpos, ypos and zpos are empty
    Well, how could the variable of size_t type be empty ?
    AFAIK size_t is not a VARIANT.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2012
    Posts
    18

    Re: String.Find() not working ...

    Hi Victor...
    Sorry - I'm just learning... what I mean by "Empty" is it doesn't return the position of "X:" ... it should be a number.
    When I hover my mouse over size_t, it states that it is an INT... doesn't that hold a number??

    Please point me in the right direction

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: String.Find() not working ...

    Quote Originally Posted by ChadReitsma View Post
    When I hover my mouse over size_t, it states that it is an INT... doesn't that hold a number??
    Variable of INT type (INT belongs to POD) always holds some number. The only question is whether this number is correct.

    And I wonder: didn't you debug your code? And how did you found out these variables "are empty"?
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2012
    Posts
    18

    Re: String.Find() not working ...

    I certainly did debug it
    I am using a Trace from inside of VC++ 2010 Express.


    I have this in my code:
    Code:
    size_t xpos;
    xpos = comData.find("X:");
    Then just below that line, I have the trace: {xpos}
    The output log says: CXX0017: Error: symbol "xpos" not found

    Yet, if i just change it to {comData} then I see the original "X:123Y:123Z:123" so I know the traces are working pproperly... it just doens't make sense.
    My guess is for some reason that find() is not returning anything...

    I should also mention, I have used string.find_first_of(); before this, and it works fine... could that be causing the other functions to hang or something?
    Last edited by ChadReitsma; April 5th, 2012 at 04:13 AM.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: String.Find() not working ...

    Quote Originally Posted by ChadReitsma View Post
    I certainly did debug it
    I am using a Trace from inside of VC++ 2010 Express.
    ...
    Then just below that line, I have the trace: {xpos}
    The output log says: CXX0017: Error: symbol "xpos" not found
    Please, don't post any pseudo code, only a real one!

    And again: doesn't VC++ 2010 Express allow you to debug?

    Note, you will never become a programmer unless you'll have learnt how to debug and will debug!
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 2012
    Posts
    18

    Re: String.Find() not working ...

    That's not pseudo man, it's exactly what I'm using... here let me post it:

    Code:
    //Extract the Angle data from the Buffer
    //IMU Outputs:
    //X:-0.36Y:0.29Z:-176.93!!!
    
    std::string comData, gyroX, gyroY, gyroZ;
    size_t xpos;
    
    comData = szBuffer;   //comData  = X:-0.36Y:0.29Z:-176.93!!!
    comData = comData.substr(0, comData.find_first_of("!") );   //Strips out the last !!! so we are left with comData = "X:-0.36Y:0.29Z:-176.93"
    
    xpos = comData.find("X:");
    look where I use find_first_of("!")... that works perfectly... so why doesn't find("X:") work on the string???

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: String.Find() not working ...

    Quote Originally Posted by ChadReitsma View Post
    That's not pseudo man, it's exactly what I'm using... here let me post it:
    Code:
    //Extract the Angle data from the Buffer
    //IMU Outputs:
    //X:-0.36Y:0.29Z:-176.93!!!
    
    std::string comData, gyroX, gyroY, gyroZ;
    size_t xpos;
    
    comData = szBuffer;   //comData  = X:-0.36Y:0.29Z:-176.93!!!
    comData = comData.substr(0, comData.find_first_of("!") );   //Strips out the last !!! so we are left with comData = "X:-0.36Y:0.29Z:-176.93"
    
    xpos = comData.find("X:");
    look where I use find_first_of("!")... that works perfectly... so why doesn't find("X:") work on the string???
    And where is the "trace" you mentioned in your previous post?
    And how did you find out that "that works perfectly"? Did you debug your code or not?
    Last edited by VictorN; April 5th, 2012 at 04:29 AM.
    Victor Nijegorodov

  9. #9
    Join Date
    Apr 2012
    Posts
    18

    Re: String.Find() not working ...

    I'm not going to use find(), it seems that find_first_of, and find_last_of actually work...

    The documentation on find() states that I can give it a String like this:
    find("THIS");

    But it seems like it is treating that as an array, where it goes through and tries to search for T, H, I, S... so I don't know...

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: String.Find() not working ...

    Quote Originally Posted by ChadReitsma View Post
    The documentation on find() states that I can give it a String like this:
    find("THIS");

    But it seems like it is treating that as an array, where it goes through and tries to search for T, H, I, S... so I don't know...
    string::find:
    Find content in string
    Searches the string for the content specified in either str, s or c, and returns the position of the first occurrence in the string.
    string::find_first_of:
    Find character in string
    Searches the string for any of the characters that are part of either str, s or c, and returns the position of the first occurrence in the string.
    Victor Nijegorodov

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: String.Find() not working ...

    Quote Originally Posted by ChadReitsma View Post
    That's not pseudo man, it's exactly what I'm using... here let me post it:

    Code:
    //Extract the Angle data from the Buffer
    //IMU Outputs:
    //X:-0.36Y:0.29Z:-176.93!!!
    
    std::string comData, gyroX, gyroY, gyroZ;
    size_t xpos;
    
    comData = szBuffer;   //comData  = X:-0.36Y:0.29Z:-176.93!!!
    comData = comData.substr(0, comData.find_first_of("!") );   //Strips out the last !!! so we are left with comData = "X:-0.36Y:0.29Z:-176.93"
    
    xpos = comData.find("X:");
    So how do we know what's really behind that "szBuffer" variable? Instead of using variables where we have no way to verify the contents, how about hard-coded strings?
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
       std::string comData;
       size_t xpos;
       comData = "XX:-0.36Y:0.29Z:-176.93!!!";
       comData = comData.substr(0, comData.find_first_of("!"));
       xpos = comData.find("X:");
       std::cout << xpos;
    }
    
    Output:
    1
    So there is nothing wrong with find(). I purposefully placed "X:" in the second position, just to verify that nothing is wrong with find().

    What is wrong is that you're giving us bad information. Again, we have no idea what "szBuffer" is or what it contains. Maybe that is the problem -- you're assuming that what is in "comData" is actually that string, when obviously, it isn't.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 5th, 2012 at 11:07 AM.

  12. #12
    Join Date
    Apr 2012
    Posts
    18

    Re: String.Find() not working ...

    Hi Guys, thanks for the help.
    It just doesn't seem to be working like that for me, so I'm not sure what's up - at any rate the find_first_of is working for me so I'm just going to use that.

    comData is a String, otherwise how would it work the original find_first_of??
    And one thing, OF COURSE I'M DEBUGGING MY CODE - Christ... I use traces and breakpoints lol

    Cheers,
    C

  13. #13
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: String.Find() not working ...

    Quote Originally Posted by ChadReitsma View Post
    And one thing, OF COURSE I'M DEBUGGING MY CODE - Christ... I use traces and breakpoints lol
    If you are debugging - can't you step into the find() to see what's wrong?
    Also, does the code posted by Paul (above) work for you?
    Can you create such a small complete app that demonstrates YOUR problem?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  14. #14
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: String.Find() not working ...

    Quote Originally Posted by ChadReitsma View Post
    And one thing, OF COURSE I'M DEBUGGING MY CODE - Christ... I use traces and breakpoints lol
    You still have not shown actual code, and in particular, you have not shown the so-called "trace: {xpos}" statement upon which all of your conclusions are drawn.

    Paul's post proves that if a statement like comData.find("X:") is not returning your expected value, then comData simply does not contain the substring "X:". So there are only two possibilites: either comData does not contain the value "X:", or it does and you are not correctly interpreting the returned value of xpos.

    As suggested in your other post on an identical question (see your post #18 in http://forums.codeguru.com/showthread.php?t=522597 ), please single-step through the affected code and confirm the actual values of all variables.

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured