Help please...output problems with printf.

#include <stdio.h>
#include <math.h>

// this function calculates the volume of a Cylinder
int main(void)
{
    int r;    // radius
    int h;    // height
    double M_PI; // pi
    
    int pOne = pow (r, 2);  
    
    // get user input of radius and height
    printf ("Enter your radius: ");
    scanf ("%f", &r);
    printf ("Enter your height: ");
    scanf ("%f", &h);

    // calculate volume
    
    volumeCylinder = pOne * M_PI * h;

    // output volume result
    
    printf ("The Volume of the Cylinder is: %lf\n", volumeCylinder);

    return 0;

}

Sample Output:

~Enter your radius: 3
~Enter your height: 2
~The Volume of the Cylinder is: 0.000000
~

The program compiles and runs but no matter what kind of printf format I do (double, float, int) I always get 0.0000..., -0.000..., or some really large number.

I don't know what could be wrong. Can anybody please help me with this? It is driving me insane.

  1. You have calculate the power of r before assigning a value to it
  2. The scanf function isn't used properly
  3. M_PI is already defined in math.h
  4. You haven't declare volumeCylinder before using it

Try this:

#include <stdio.h>
#include <math.h>

// this function calculates the volume of a Cylinder

int main(void)
{
    int r;    // radius
    int h;    // height
    double volumeCylinder; 

    // get user input of radius and height
    printf ("Enter your radius: ");
    scanf ("%d", &r);
    printf ("Enter your height: ");
    scanf ("%d", &h);

    // calculate volume

    int pOne = pow (r, 2);     

    volumeCylinder = pOne * M_PI * h;

    // output volume result
    
    printf ("The Volume of the Cylinder is: %lf\n", volumeCylinder);

    return 0;

}

Regards

Thanks for your help! :slight_smile:

It didn't even occur to me to check placement. Oops.

You are using the %f format to scanf, but storing to an (int). (%f requires a (double) to receive the result. GNU C with -Wformat would warn you of this mistake.)