This is a simple problem, but I can't see it:
char *s = "f 8.649292" ; double d ; sscanf( s, "f %f", &d ) ; printf( "d is %f\n", d ) ;
Why is d
not containing the double value 8.649292
?
To read a double, supply scanf with a format string containing the conversion specification %lf (that's a lower case L, not a one), and include a double variable preceded by an ampersand as the second parameter.
The sscanf() function in C++ is used to read the data from string buffer.
The sscanf() function returns the number of fields that were successfully converted and assigned. The return value does not include fields that were read but not assigned. The return value is EOF when the end of the string is encountered before anything is converted.
Oh wait, nevermind. d needs to be a float
.
And to make it work you could use %lf
for a double
char *s = "f 8.649292 " ; double d ; sscanf( s, "f %lf", &d ) ; printf( "d is %lf\n", d ) ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With