atof() failure case

S

Sreekanth

Hello,
Am trying to convert a string to float. Am using atof() for that purpose.
But the return value for atof is same for the string "0.0" and for some
invalid input "Invalid". Can any body suggest me the way to differentiate an
invalid float number string and a 0.0.
TIA,
Sreekanth.
 
I

Irrwahn Grausewitz

Sreekanth said:
Hello,
Am trying to convert a string to float. Am using atof() for that purpose.
But the return value for atof is same for the string "0.0" and for some
invalid input "Invalid". Can any body suggest me the way to differentiate an
invalid float number string and a 0.0.

Instead of atof use strtod.

Regards
 
S

Sreekanth

Thanks, But it also returns 0 in both cases.. I want to differentiate an
invalid string and a Zero string in a string to float conversion mechanism.
 
J

Jens.Toerring

Thanks, But it also returns 0 in both cases.. I want to differentiate an
invalid string and a Zero string in a string to float conversion mechanism.

Well, that's why you should use strtod():

double strtod(const char *nptr, char **endptr);

If you got an illegal argument *endptr and nptr are identical after
the call of the function. On legal input what endptr points to is
a pointer to the part of the input string where the conversion
stopped and it differs from nptr.
Regards, Jens
 
S

Sreekanth

Thanks Gentleman.. Working..

mechanism.

Well, that's why you should use strtod():

double strtod(const char *nptr, char **endptr);

If you got an illegal argument *endptr and nptr are identical after
the call of the function. On legal input what endptr points to is
a pointer to the part of the input string where the conversion
stopped and it differs from nptr.
Regards, Jens
 
D

Dan Pop

In said:
Thanks, But it also returns 0 in both cases..

It also provides enough clues to make the disambiguation trivial.
I want to differentiate an
invalid string and a Zero string in a string to float conversion mechanism.

Either use the clues provided by strtod, or use atof and do some minimal
parsing of the string yourself, when the result of the conversion is 0.

Dan
 
C

CBFalconer

Sreekanth said:
Am trying to convert a string to float. Am using atof() for that
purpose. But the return value for atof is same for the string "0.0"
and for some invalid input "Invalid". Can any body suggest me the
way to differentiate an invalid float number string and a 0.0.

strtod();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top