atof() failure case

Discussion in 'C Programming' started by Sreekanth, Apr 16, 2004.

  1. Sreekanth

    Sreekanth Guest

    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.
    Sreekanth, Apr 16, 2004
    #1
    1. Advertising

  2. "Sreekanth" <> wrote:
    >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
    --
    Irrwahn Grausewitz ()
    welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
    Irrwahn Grausewitz, Apr 16, 2004
    #2
    1. Advertising

  3. Sreekanth

    Sreekanth Guest

    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.



    "Irrwahn Grausewitz" <> wrote in message
    news:...
    > "Sreekanth" <> wrote:
    > >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
    > --
    > Irrwahn Grausewitz ()
    > welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
    > clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    > clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
    Sreekanth, Apr 16, 2004
    #3
  4. Sreekanth

    -berlin.de Guest

    Sreekanth <> wrote:
    > "Irrwahn Grausewitz" <> wrote in message
    > news:...
    >> "Sreekanth" <> wrote:
    >> >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.
    >>


    > 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
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Apr 16, 2004
    #4
  5. Sreekanth

    Sreekanth Guest

    Thanks Gentleman.. Working..

    <-berlin.de> wrote in message
    news:c5oa33$418ks$...
    > Sreekanth <> wrote:
    > > "Irrwahn Grausewitz" <> wrote in message
    > > news:...
    > >> "Sreekanth" <> wrote:
    > >> >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.
    > >>

    >
    > > 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
    > --
    > \ Jens Thoms Toerring ___ -berlin.de
    > \__________________________ http://www.toerring.de
    Sreekanth, Apr 16, 2004
    #5
  6. Sreekanth

    Dan Pop Guest

    In <c5o94k$2ph$> "Sreekanth" <> writes:

    >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

    >"Irrwahn Grausewitz" <> wrote in message
    >news:...
    >> "Sreekanth" <> wrote:
    >> >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.


    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Apr 16, 2004
    #6
  7. Sreekanth

    CBFalconer Guest

    Sreekanth wrote:
    >
    > 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();

    --
    A: Because it fouls the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    CBFalconer, Apr 16, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Drew

    Why doesn't atof work?

    Drew, Jan 20, 2004, in forum: C++
    Replies:
    10
    Views:
    4,752
    Jeff Schwab
    Jan 23, 2004
  2. Pete C.

    atof() and _tstof() in VC6.0

    Pete C., Jun 24, 2004, in forum: C++
    Replies:
    9
    Views:
    7,253
    Ian Semmel
    Jun 24, 2004
  3. Replies:
    2
    Views:
    3,130
  4. Sharon
    Replies:
    4
    Views:
    984
    CBFalconer
    Nov 17, 2003
  5. XZ
    Replies:
    15
    Views:
    806
    Peter Nilsson
    Jun 30, 2005
Loading...

Share This Page