-
April 5th, 2012, 03:35 AM
#1
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
-
April 5th, 2012, 03:47 AM
#2
Re: String.Find() not working ...
Originally Posted by ChadReitsma
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
-
April 5th, 2012, 03:54 AM
#3
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
-
April 5th, 2012, 03:59 AM
#4
Re: String.Find() not working ...
Originally Posted by ChadReitsma
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
-
April 5th, 2012, 04:04 AM
#5
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.
-
April 5th, 2012, 04:11 AM
#6
Re: String.Find() not working ...
Originally Posted by ChadReitsma
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
-
April 5th, 2012, 04:18 AM
#7
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???
-
April 5th, 2012, 04:26 AM
#8
Re: String.Find() not working ...
Originally Posted by ChadReitsma
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
-
April 5th, 2012, 04:29 AM
#9
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...
-
April 5th, 2012, 04:41 AM
#10
Re: String.Find() not working ...
Originally Posted by ChadReitsma
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
-
April 5th, 2012, 11:04 AM
#11
Re: String.Find() not working ...
Originally Posted by ChadReitsma
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.
-
April 5th, 2012, 04:38 PM
#12
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
-
April 5th, 2012, 04:48 PM
#13
Re: String.Find() not working ...
Originally Posted by ChadReitsma
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...
-
April 5th, 2012, 05:50 PM
#14
Re: String.Find() not working ...
Originally Posted by ChadReitsma
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|