Thread: atoi and floating point errors

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32

    atoi and floating point errors

    Hi all! I'm trying to make a really simple program in c which when given 3 values plonks them in a formula and outputs another value.

    Here's the code before I get onto the problem

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            int wr2;
            int fst2;
            int rst2;
            int gear;
            char wr1[5];
            char fst1[5];
            char rst1[5];
    
            printf("Please enter wheel radius\n");
            scanf("%d",wr1); 
            wr2=atoi(wr1);
            printf("Please enter number of teeth on front sprocket\n");
            scanf("%d",fst1);
            fst2=atoi(fst1);
            printf("Please enter number of teeth on rear sprocket\n");
            scanf("%d",rst1);
            rst2=atoi(rst1);
    
            gear=wr2*(fst2/rst2);
            printf("Wheel Radius %d inches, front teeth %d, rear teeth %d gives gear %d inches.\n",wr2,fst2,rst2,gear);
            return(0);
    }
    When run the program stores the 3 values entered and either outputs 1 of 2 things depending on the coding. If gear is set as an int i get a floating point error, is gear is set as float it always outputs 54 no matter what values are entered. Its a really basic error but as I've just started C I cannot figure out why it wont work. Any help is much appreciated.

    Native
    Last edited by Native; 10-14-2010 at 02:46 PM. Reason: tidying code

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,547
    Why are you trying to use character arrays for your input?

    Your scanf format string is incorrect to get a character. It should be
    Code:
    ("%s",wr1);
    to get a character array.

    But you could just get floating point numbers with scanf instead of character arrays and then converting to numbers.

    Code:
       double wr1 = 0;
       scanf("%f",&wr1);
    In the following section of code
    Code:
        gear=wr2*(fst2/rst2);
    Because you are using Integers you will be throwing away any decimal points.

    I would suggest that you convert everything to doubles. This should solve most of your problems.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    will try this, thank you!

  4. #4
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    ok so it looks like this now

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            double wr;
            double fst;
            double rst;
            double gear;
    
            printf("Please enter wheel radius\n");
            scanf("%f",&wr);
            printf("Please enter number of teeth on front sprocket\n");
            scanf("%f",&fst);
            printf("Please enter number of teeth on rear sprocket\n");
            scanf("%f",&rst);
    
            gear=wr*(fst/rst);
            printf("Wheel Radius %f inches, front teeth %f, rear teeth %f gives gear %f inches.\n",wr,fst,rst,gear);
    
            return(0);
    }
    its giving me a load of zeros, was reading up on integers and read to us %f, im assuming thats why its not workin tho?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Doubles are %lf in printf(). (long float == double).
    Last edited by Adak; 10-14-2010 at 07:40 PM.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    ah, thanks - will try that instead

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    scanf() format specifier for double is %lf.
    for printf() either float or double is %f but C99 accepts %lf as double.
    Question 12.9

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    scanf doesn't check for numeric overflow, so it's inherently unsuited for getting numeric input.

    The best way to get numeric input is to read it in as a string first with fgets or similar, and then use strtod to convert it to a double. If you want a float instead and strtof is available to you (C99 function), then you can use that instead. Otherwise it's not terrible hard to make your own function that uses strtod for the initial conversion.

    Code:
    #include <errno.h>
    #include <math.h>
    #include <float.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define INPUT_SIZE 80
    
    extern int errno;
    
    /*
     * Returns 0 on failure and 1 on success.
     */
    int
    string_to_double(const char *s, double *result, char **end)
    {
        double d;
    
        errno = 0;
        d = strtod(s, end);
    
        if (s == *end || ((d == 0 || d == HUGE_VAL) && errno == ERANGE))
        {
            return 0;
        }
    
        *result = d;
        return 1;
    }
    
    /*
     * Returns 0 on failure and 1 on success.
     */
    int
    string_to_float(const char *s, float *result, char **end)
    {
        double d;
    
        if (!string_to_double(s, &d, end))
        {
            return 0;
        }
    
        if (d > FLT_MAX || d < FLT_MIN)
        {
            return 0;
        }
    
        *result = (float)d;
        return 1;
    }
    
    int
    main(void)
    {
        char input[INPUT_SIZE];
        char *end;
        float f;
    
        if (!fgets(input, INPUT_SIZE, stdin))
        {
            puts("Invalid input!");
            return EXIT_FAILURE;
        }
    
        if (!string_to_float(input, &f, &end))
        {
            puts("Invalid input!");
            return EXIT_FAILURE;
        }
    
        printf("Number is: %f\n", f);
    
        return EXIT_SUCCESS;
    }

  9. #9
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    It worked!
    Thank you

    here is the finished product

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            double wr;
            double fst;
            double rst;
            double gear;
            
            printf("Please enter wheel radius\n");
            scanf("%lf",&wr); 
            printf("Please enter number of teeth on front sprocket\n");
            scanf("%lf",&fst);
            printf("Please enter number of teeth on rear sprocket\n");
            scanf("%lf",&rst);
    
            gear=wr*(fst/rst);
            printf("Wheel Radius %lf inches, front teeth %lf, rear teeth %lf gives gear %lf inches.\n",wr,fst,rst,gear);
            return(0);
    }

Popular pages Recent additions subscribe to a feed