atof() failure case

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sreekanth

    atof() failure case

    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.


  • Irrwahn Grausewitz

    #2
    Re: atof() failure case

    "Sreekanth" <sreekanth@yaho o.com> wrote:[color=blue]
    >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.[/color]

    Instead of atof use strtod.

    Regards
    --
    Irrwahn Grausewitz (irrwahn33@free net.de)
    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

    Comment

    • Sreekanth

      #3
      Re: atof() failure case

      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" <irrwahn33@free net.de> wrote in message
      news:728v70l4a9 729m9gficpg4hkp 267c1rd50@4ax.c om...[color=blue]
      > "Sreekanth" <sreekanth@yaho o.com> wrote:[color=green]
      > >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[/color][/color]
      an[color=blue][color=green]
      > >invalid float number string and a 0.0.[/color]
      >
      > Instead of atof use strtod.
      >
      > Regards
      > --
      > Irrwahn Grausewitz (irrwahn33@free net.de)
      > 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[/color]


      Comment

      • Jens.Toerring@physik.fu-berlin.de

        #4
        Re: atof() failure case

        Sreekanth <sreekanth@yaho o.com> wrote:[color=blue]
        > "Irrwahn Grausewitz" <irrwahn33@free net.de> wrote in message
        > news:728v70l4a9 729m9gficpg4hkp 267c1rd50@4ax.c om...[color=green]
        >> "Sreekanth" <sreekanth@yaho o.com> wrote:[color=darkred]
        >> >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[/color][/color]
        > an[color=green][color=darkred]
        >> >invalid float number string and a 0.0.[/color]
        >>
        >> Instead of atof use strtod.
        >>[/color][/color]
        [color=blue]
        > 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.[/color]

        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 ___ Jens.Toerring@p hysik.fu-berlin.de
        \______________ ____________ http://www.toerring.de

        Comment

        • Sreekanth

          #5
          Re: atof() failure case

          Thanks Gentleman.. Working..

          <Jens.Toerring@ physik.fu-berlin.de> wrote in message
          news:c5oa33$418 ks$1@uni-berlin.de...[color=blue]
          > Sreekanth <sreekanth@yaho o.com> wrote:[color=green]
          > > "Irrwahn Grausewitz" <irrwahn33@free net.de> wrote in message
          > > news:728v70l4a9 729m9gficpg4hkp 267c1rd50@4ax.c om...[color=darkred]
          > >> "Sreekanth" <sreekanth@yaho o.com> wrote:
          > >> >Hello,
          > >> >Am trying to convert a string to float. Am using atof() for that[/color][/color][/color]
          purpose.[color=blue][color=green][color=darkred]
          > >> >But the return value for atof is same for the string "0.0" and for[/color][/color][/color]
          some[color=blue][color=green][color=darkred]
          > >> >invalid input "Invalid". Can any body suggest me the way to[/color][/color][/color]
          differentiate[color=blue][color=green]
          > > an[color=darkred]
          > >> >invalid float number string and a 0.0.
          > >>
          > >> Instead of atof use strtod.
          > >>[/color][/color]
          >[color=green]
          > > 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[/color][/color]
          mechanism.[color=blue]
          >
          > 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 ___ Jens.Toerring@p hysik.fu-berlin.de
          > \______________ ____________ http://www.toerring.de[/color]


          Comment

          • CBFalconer

            #6
            Re: atof() failure case

            Sreekanth wrote:[color=blue]
            >
            > 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.[/color]

            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?


            Comment

            • Dan Pop

              #7
              Re: atof() failure case

              In <c5o94k$2ph$1@n ews.mch.sbs.de> "Sreekanth" <sreekanth@yaho o.com> writes:
              [color=blue]
              >Thanks, But it also returns 0 in both cases..[/color]

              It also provides enough clues to make the disambiguation trivial.
              [color=blue]
              >I want to differentiate an
              >invalid string and a Zero string in a string to float conversion mechanism.[/color]

              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
              [color=blue]
              >"Irrwahn Grausewitz" <irrwahn33@free net.de> wrote in message
              >news:728v70l4a 9729m9gficpg4hk p267c1rd50@4ax. com...[color=green]
              >> "Sreekanth" <sreekanth@yaho o.com> wrote:[color=darkred]
              >> >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[/color][/color]
              >an[color=green][color=darkred]
              >> >invalid float number string and a 0.0.[/color]
              >>
              >> Instead of atof use strtod.[/color][/color]

              --
              Dan Pop
              DESY Zeuthen, RZ group
              Email: Dan.Pop@ifh.de

              Comment

              Working...