View Full Version : Easy C/C++ question
phdmom
02-21-2003, 05:45 PM
If I'm using getline() to read in a line from a textfile, and then substr() to break it apart into strings, how can I convert one of those strings to a number? atof doesn't work.
Thanks.
:wall:
Wino In Training
02-21-2003, 05:54 PM
It's been a looooooong time since I did any C/C++..... but wouldn't sscanf handle this?
GadgetGeek
02-21-2003, 05:56 PM
unfortunately my C is a little rusty, but I think you could use scanf(stringwithnumberinit,"%d",dNewValue); or something like that
phdmom
02-21-2003, 06:59 PM
I tried that first, but it caused me problems with another field of the data, which may or may not contain a space. Two rows might be:
Z 987432 1482.25
ZA234578 2345.82
I didn't know how to work that into scanf.
GadgetGeek
02-21-2003, 07:36 PM
does the string always end the same way? Maybe strrev first, then parse out the numbers as strings, another strrev to reorient the numbrs then sscanf? Another option would be to find the first decimal point, then step backwards to find the first space and work from there. Or maybe look for the first space after positon 3 or 4 or whatever.
Cho Da
02-21-2003, 07:45 PM
Since you aske dabout C++, why don't you use the stream library?
source >> text >> float >> text1;
oedipus rex
02-21-2003, 09:33 PM
mom, could you post a snippet of some of your code in context?
Voron
02-22-2003, 12:50 AM
And why wouldn't atof work? I assume that you parsed the string and have the float in a buffer... What compiler are you using?
vBulletin® v3.7.6, Copyright ©2000-2013, Jelsoft Enterprises Ltd.