Thread: is there a problem in this method of casting?

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    is there a problem in this method of casting?

    i want to cast "55.77" which is a string to 55.77 which is a float

    sscanf(str,"%5f",fl);

    fl will hold 55.77
    correct?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    sscanf requires pointers to the variables it will put the data in...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    sscanf(str,"%5f",&fl);

    correct
    fl is float or
    long float or something
    ?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    float - for the way you're using it.
    double if you're doing
    Code:
    sscanf(str, "%lf", &fl);

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need two digit approximation
    float is only one digit after the point
    and why you wrote %lf
    ?

    Code:
    double entered_val;
    sscanf(str,"%f", &entered_val);
    is this code correct?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> is this code correct?

    No. Remember that the format specifier tells sscanf how large the variable is. Since %f is for floats the function will assume the variable is sizeof(float) instead of sizeof(double). In this case, you've only trashed the variable, but imagine if you had:

    Code:
    char c;
    sscanf(str,"%d", &c);
    In this case the function expects sizeof(int) bytes at the address of 'c', and as a result the bytes following 'c' get trashed!

    The moral: be very careful in matching your variables with format specifiers.
    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;
    }

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so what specifier to use for a double variable
    if i want two places after a point proximity

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> so what specifier to use for a double variable

    Don't you have documentation on the library functions? You should always have a copy nearby.

    >> if i want two places after a point proximity

    sscanf doesn't deal with precision. It simply reads the string and stuffs the entire number into the variable. What you need to do is control the way the variable is *written* to the string (eg: 'str' in your code). For instance, if you're using sprintf to format the string, just use the width modifier (refer to sprintf reference for a full listing of options) to control the precision of the output.
    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;
    }

  9. #9
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    %.2f
    Spidey out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with header file method
    By Beowolf in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2007, 10:10 PM
  2. Problem with RTTI and a factory method
    By philvaira in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2007, 03:19 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Casting Problem
    By Sebastiani in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2002, 02:06 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM