View Full Version : Easy C/C++ question
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.
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?
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
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
I didn't know how to work that into scanf.
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.
02-21-2003, 07:45 PM
Since you aske dabout C++, why don't you use the stream library?
source >> text >> float >> text1;
02-21-2003, 09:33 PM
mom, could you post a snippet of some of your code in context?
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.