Click here to Skip to main content
14,774,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok so I'm trying to figure out how to take a part of a string from between delimiters and assign another string to it, which I then plan to convert to a float type. This is what I came up with so far but I've never really used string functions in C++ all that much before (it's so much easier in VB) so I seem to be having problems with string::find() if someone could let me know what I'm doing wrong or if there's a much better and/or easier way to do it please let me know :)

Am I using sting::find() wrong? is it the wrong function for this context and if so what am I supposed to do? I'm not going to lie, I find it difficult to read a lot of the reference pages for libraries since I haven't been taught a lot of the technical terms though I've picked a lot up, self teaching as a whole, I just like to get an explanation every now and then.

void load(int vNum, float& toX, float& toY, float& toZ)
{
     std::string line = "XSomethingY fromZ an N fstream";
     std::string lCrd;
     //trying to take out a specific peice of a string from between delimiter
     //and one in 'from the left'
     lCrd.append(line,1,line.find("Y")-1);//seems to crash the program (Y is my delimiter)
     //so lCrd should be "Something" or perhaps "SomethingY" at this point
     //debugger says 'line.find = ?' what does this mean/is it useful at all?
     //then would convert to float and set toX = 'float(lCrd)'
     lCrd = "";
     lCrd.append(line,line.find("Y"),line.find("Z")-line.find("Y"));
}
Posted
Updated 5-Nov-13 16:27pm
v3
Comments
Aescleal 6-Nov-13 4:30am
   
As well as following Garth's advice to split things up a bit and include some error checking look at using the string range constructor, std::string::substr and operator+( const string &, const str &), they might make your code slightly less WTFish to read!

Don't be afraid of local variables, they can make things far easier to read and won't slow your code down in the general case.

I know it's 'sexy' to do this sort of thing

lCrd.append(line,1,line.find("Y")-1);//seems to crash the program (Y is my delimiter)


but the issue with it is, if

line.find("Y")-1


doesnt evaluate 'nicely' or is out of range, its hard to spot/correct, even using a debugger.

I would start by 'coding defensively', even 'basic/gumby style' so you could do somethling like

int foundpos = line.find("Y")-1;
assert (foundpos < 0)
assert (foundpos > line.length()) 


(thats just a rough idea btw, you could use an 'if' .. the point is, you then get a chance to test the string positions && handle unexpected values .. either that, or start using 'try/catch', and/or have a different way of sanitising your strings before you get to this point)

... so you know what Im going to say about

lCrd.append(line,line.find("Y"),line.find("Z")-line.find("Y"));


dont you :-)

'g'
   
Comments
James kingswell 5-Nov-13 21:30pm
   
A very good point sir. I just get used to doing things like that as I shorten my code.. Perhaps it's a bad habit and should probably always do things this way before hand. Thanks :)
James kingswell 5-Nov-13 22:17pm
   
And it was as simple as that.. the problem resided in my assignment of lCrd -_- I feel slightly embarrassed.. oh well lesson learned, if I've managed to find the local area of the bug simplify the area down to it's components before posting a question.. Thanks again, best advice I could've gotten. :)
int first = string.Find(_T("delimiter1"));
int last = string.Find(_T("delimiter2"));
CString temp = string.Mid(first,(last-first));


I worked in this way to get the string between two delimiters.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900