scanf() and doubles?

This is a discussion on scanf() and doubles? within the C Programming forums, part of the General Programming Boards category; I am having an issue with the following program. This is just a simple program that calculates the surface area/volume ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    16

    scanf() and doubles?

    I am having an issue with the following program. This is just a simple program that calculates the surface area/volume of a cylinder. For some reason this executes JUST fine in CodeBlocks. Everything works exactly as it should and I get the correct values.

    However, when I try to run it in the IDE that my professor forces us to use I always get 0 for values. If I change all the variables to int and use %d it will compile fine in both, but I lose accuracy. Double is required. Is %lf the correct scanf() function to use for doubles? I have tried many different things and I cannot get it to work properly in the IDE for my class.

    Here is the following code:

    Code:
    #include <stdio.h>
    #define PI 3.141593
    
    
    double computation(double l, double r)
    {
        if (2*r >= l)
        {
            printf("These dimensions do not define a valid tank");
        }
        else
        {
        double volCYL = (PI * r * r * l);
        double volSPH = (4.0/3.0 * PI * r * r * r);
        double surfCYL = (2 * PI * r * l);
        double surfSPH = (4 * PI * r * r);
    
    
        printf("\n");
        printf("A cylindrical tank of length %.3lf and radius %.3lf with inverted spherical\n", l,r);
        printf("caps has\n");
        printf("volume: %.3lf\n", volCYL-volSPH);
        printf("and\n");
        printf("surface area: %.3lf\n", surfCYL+surfSPH);
        printf("\n");
        printf("Program Termination.\n");
        }
        return 0;
    }
    
    
    double length = 0;
    double radius = 0;
    
    
    
    
    int main()
    {
        printf("ACME Truck Company\n");
        printf("\n");
        printf("This program will calculate the volume and surface area of the x-11\n");
        printf("cylindrical tank\n");
        printf("Enter the length of the tank (feet)");
        scanf("%lf",&length);
    
    
        while (length < 10 || length > 20)
        {
            printf("Invalid value\n");
            printf("Please enter a value between 10 and 20:");
            scanf("%lf", &length);
            getchar();
        }
    
    
        printf("Enter the radius of the tank (feet)");
        scanf("%lf", &radius);
    
    
        while (radius < 3 || radius > 6)
        {
            printf("Invalid value\n");
            printf("Please enter a value between 3 and 6:");
            scanf("%lf", &radius);
            getchar();
        }
    
    
        computation(length,radius);
        return 0;
    }

    Any help would be much appreciated!
    Last edited by Frankie15; 09-27-2011 at 05:44 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,628
    Quote Originally Posted by Frankie15 View Post
    I am having an issue with the following program. This is just a simple program that calculates the surface area/volume of a cylinder. For some reason this executes JUST fine in CodeBlocks. Everything works exactly as it should and I get the correct values.

    However, when I try to run it in the IDE that my professor forces us to use I get garbage numbers. If I change all the variables to int and use %d it will compile fine in both, but I lose accuracy. Double is required. Is %lg the correct scanf() function to use for doubles? I have tried many different things and I cannot get it to work properly in the IDE for my class.
    Yes, "%lg" is correct for doubles for scanf, but you only need "%g" for printf according to printf(3): formatted output conversion - Linux man page.

    EDIT: If that doesn't fix it, tell us what compiler you're using with Code::Blocks that works, and what IDE/compiler you have to use for school.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Thank you SO much anduril.

    Ok, I used %lg and %g (ignore code in updated original post. I thought it may have been lf instead of lg so I updated.)
    Using %g instead of %lg worked PERFECTLY. Again, thank you!

    %lg for scan and %g is working fine now to get whole number values, BUT I am having trouble getting it to display to three decimal places. Wouldn't you just type:

    "%.3g"?

    EDIT: It seems using %.3f will work for printing 3 decimal places. Is it acceptable to print an "lg" variable with "%f?
    Last edited by Frankie15; 09-27-2011 at 06:10 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,628
    Yes, it's acceptable to use %f for your output. Actually, I almost always see %f used for floats/doubles, and rarely, if ever, see %g or %e, though to be fair, I don't write scientific code, so I don't need scientific notation. Read the link in my previous post carefully, particularly the parts for the "f,F" and "g,G" specifiers if you want all the details.

    EDIT: and you're welcome.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Thanks for the link as well. I will use that to review for sure.

    Thanks again for your help. Appreciate you taking the time to do so!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubles variable
    By samuelanonymous in forum C Programming
    Replies: 3
    Last Post: 07-12-2007, 08:17 AM
  2. Portable Doubles
    By EvilGuru in forum C Programming
    Replies: 31
    Last Post: 08-31-2006, 08:44 PM
  3. Using Doubles
    By jay kay in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2005, 01:14 PM
  4. Problem with doubles
    By Asbestos in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2005, 05:47 PM
  5. Please help - dividing doubles
    By hunterdude in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2004, 01:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21