Thread: atof

  1. #1
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96

    atof

    Atof function returns 0 when there is some error during conversion AND when input string is "0", "0.0", ".0" or whatever. That's what I understand form help. How can I know whether user really inputted zero or there was an error?
    Please excuse my poor english...

  2. #2
    Unregistered
    Guest

    Lightbulb

    well, Im not sure, but atof might set errno to some value if an error occured. I'd check myself, but I recently had some computer trouble, and It would be just a lot of hassle. Try this



    Code:
    #include <stdio.h>
    #include <whatever header file atof is in>
    #include <iostream.h>  //I guess this is the C++ board, 
    // so i had better include this
    
    main()
    {
          int err = errno;
          atof( "aslfkhas;lf" );  // junk data to see if errno changes
          if( err != errno )
               cout << "atof sets errno on error!"
          else
               cout << "nope";
          return 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Why not use strcmp() to test for it?



    if(atof(string) || !strcmp(string,"0")
    {
    /// Process good Data
    }
    else
    {
    /// print an error, set flag, etc.
    }


    Thus, typing "0" into the string will pass the "!strcmp(string,"0")"
    test whereas hitting "enter" or typing garbage (which passes the "atof(string)" test ) will not.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Why not use strcmp() to test for it?



    if(atof(string) > 0 || !strcmp(string,"0")
    {
    /// Process good Data
    }
    else
    {
    /// print an error, set flag, etc.
    }


    Thus, typing "0" into the string will pass the "!strcmp(string,"0")"
    test whereas hitting "enter" or typing garbage (which passes the "atof(string)" test ) will not.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    And what if user enters 0.0, 0e-0, .000 or something? I'd like this all to work, no restrictions in how he specifies zero.
    Please excuse my poor english...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atof function question
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-13-2008, 09:43 PM
  2. problem with atof
    By ssharish in forum C Programming
    Replies: 17
    Last Post: 03-24-2005, 07:53 AM
  3. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  4. atof()
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2003, 10:08 AM
  5. how do atof, atoi, and atol work?
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 09:25 PM