CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2004
    Location
    San Diego, CA
    Posts
    14

    strings to floating point number

    I found this subset of code in the frequently asked questions section and decided to try it out. I found a couple of problems with it and was wondering if anyone can help me understand.

    const char* str_int = "777";
    const char* str_float = "333.3";
    int i;
    float f;
    if(EOF == sscanf(str_int, "%d", &i)){
    //error
    }
    if(EOF == sscanf(str_float, "%f", &f)){
    //error
    }

    First of all, if the string value is "hello", sscanf returns 0, not EOF. So I don't understand why the author recommends checking for EOF. If the input string is not a number, then I want to take the error path.

    Second, if I define the variable f to be of type double instead of float, the sscanf function will fail. Why? I thought double is a float but just twice the size. I searched in the MSDN for format specifiers and found nothing to indicate a special specifier for a double. Does anyone know what is going on here? When I say fail, I mean that the function returns 0 and the variable f will be garbage as if sscanf refuses to even try to convert the string to a double. However, it works great as long as the variable f is declared as a float.

    Best Regards,

    Shawn Fox

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: strings to floating point number

    Please use strtod() to convert from a string to a floating point value. The sscanf() function has a variety of pitfalls. There was a thread just last week on why sscanf() is a function that should not be used, and there are much better replacements (such as strtod).

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2004
    Location
    San Diego, CA
    Posts
    14

    Re: strings to floating point number

    Thanks, I guess you're right. strtod will work fine for me. I sure wish someone would update that FAQ topic with new info. That led me pretty far down the wrong path.

    Best Regards,
    Shawn

  4. #4
    Join Date
    Mar 2005
    Posts
    17

    Re: strings to floating point number

    for string -> float (double), I personally use atof()
    for float -> string I use sprintf()

    just how I do it though.

  5. #5
    Join Date
    Feb 2004
    Location
    San Diego, CA
    Posts
    14

    Re: strings to floating point number

    Thanks. I have used atof and atoi before. However, in this case it is critical that I know if the string can be completely converted. I need to know if the string is something like this, "hello" as opposed to "35.6", or "35 dollars".

    My function needs to perform some error logic if the function cannot convert the entire input string. strtol and strod both provide that functionality. With atoi and atof, you had better know that the input string is completely convertable or ensure that it is not critical if it cannot be. You know what I mean? Also, atoi and atof return 0 for different situations making it impossible to use these functions if you need to do any error testing to determine if the whole string was converted or not.

    Thanks for all the help. I definitely found what I was looking for.

    Best Regards,

    Shawn

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: strings to floating point number

    In addition, you might want to take a look at the following FAQ...

  7. #7
    Join Date
    Feb 2004
    Location
    San Diego, CA
    Posts
    14

    Re: strings to floating point number

    Yes, I've seen it. That was the FAQ I referred to in the first post. It is incorrect and needs to be changed. The information on sscanf does not make sense at all. The FAQ makes it sound like scanf will detect strings that aren't numbers and that isn't true. I've tried it numerous ways and have never seen sscanf return EOF.

    Shawn

  8. #8
    Join Date
    Oct 2002
    Posts
    1,134

    Re: strings to floating point number

    Without attempting to justify what's in the FAQ, from MSDN for "scanf":
    The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.
    And I think to read a "double" value your format would have to be "%Lf"
    Regards
    Robert Thompson

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured