-1

I am doing a relatively simple program to calculate a length * width * height into cubic inches. I am supposed to get an answer like XXXXXXX.XX but instead the compiler gives me 0.000000

I have no other errors. My main question is how to get the numbers to calculate?

#include <stdio.h>

double length;
double width;
double height;

// This is the volume in cubic inches.
double VolumeCubicInch;

// This is the volume in cubic feet.
double VolumeCubicFeet;

int main() {
    // Ask the user to enter the length width and height of a box (in inches).

    // First print asks user to enter the length of a box in inches.
    printf("Please enter the length of a box in inches.\n");
    // The user reads in the length number of the box in inches.
    scanf("%lf", &length);

    // Second print asks user for the width of the box in inches.
    printf("Now please enter the width of the box in inches.\n");
    // The user reads in the width number in inches.
    scanf("%lf", &width);

    // Then the third print asks user for the height of the box in inches.
    printf("Please enter the height of box in inches.\n");
    // The user reads in the height of the box in inches.
    scanf("%lf", &height);

    // Calculate the volume of the box, in cubic inches and output the result.
    // Using a newly created variable called VolumeCubicInch, it will allow the        calculation
    // of the box's volume to be calculated in cubic inches.
    // Length output given: 15.8
    // Width output given: 23.34
    // Height output given: 75.345
    VolumeCubicInch = length * width * height;
    // The resulted volume in cubic inches will be outputted using a print   statement.
    // Output should be: The volume is XXXXXXX.XX cubic inches.
    printf("The volume is %lf cubic inches.\n", &VolumeCubicInch);

    // Calculate the volume in cubic feet, and output the result.
    // Using the variable VolumeCubicFeet will produce the volume in cubic feet.
    // VolumeCubicFeet = ;
    // The value or result of the volume in cubic feet will be outputted using     the variable VolumeCubicFeet.
    // The output should be: The volume is XXXX.XX cubic feet.
    // printf("The volume is %lf cubic feet.\n", &VolumeCubicFeet);

    // Note that a box that is 12 x 12 x 12 inches is 1.0 cubic feet.
    // *Be sure that your program gets that answer.*

    system("PAUSE");
    return 0;
}
John
  • 13
  • 2
  • 6
  • 5
    That's really too bad. Unfortunately, without seeing your code there's not much anyone can do to help you. Best of luck. – Bob Jarvis - Reinstate Monica Jan 23 '17 at 22:50
  • Please show use what you have done so far. – Code-Apprentice Jan 23 '17 at 22:51
  • It is not the compiler that gives you 0.000000. It is your program. – DYZ Jan 23 '17 at 22:51
  • 3
    try `printf("The volume is %f cubic inches.\n", VolumeCubicInch);` – yano Jan 23 '17 at 22:56
  • 2
    Along with what yano says, you should compile with warnings enabled to see your compiler complain about how you're currently using `printf`. – Kevin Jan 23 '17 at 22:57
  • Thank you yano I tried and same thing happens – John Jan 23 '17 at 22:58
  • Kevin, I'm not sure I understand "compile with warnings enabled". I'm using DevC – John Jan 23 '17 at 22:59
  • Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the [About] page and also [How do I ask questions here?](http://stackoverflow.com/help/how-to-ask) and [What do I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) – Jonathan Leffler Jan 24 '17 at 01:32
  • @John: you can accept one of the answers by clicking on the grey checkmark below its score. – chqrlie Feb 04 '17 at 22:36

2 Answers2

4

You pass the address of the result instead of its value:

printf("The volume is %lf cubic inches.\n", &VolumeCubicInch);

You should instead pass the value and specify 2 decimal places:

printf("The volume is %.2f cubic inches.\n", VolumeCubicInch);

Notes:

  • the l modifier is necessary for scanf to convert to double format instead of float. printf always promotes float values to double, so the %f format is used for both and the l is ignored if specified.
  • for type long double, you would use the %Lf conversion specifier in both scanf and printf.
  • you can specify the number of decimal places by passing a precision field after a decimal point between the % and the f format character.
chqrlie
  • 98,886
  • 10
  • 89
  • 149
  • @Kevin: you are correct. The `l` is necessary for `scanf()` but if ignored by `printf`. – chqrlie Jan 23 '17 at 23:00
  • Thank you this solved it!! – John Jan 23 '17 at 23:03
  • Thank you for the notes I was about to ask about the float vs double. Is it possible to add a note about using long float / long double? – John Jan 23 '17 at 23:07
  • @John: there is no such thing as `long float`. For `long double`, you should use the `L` modifier. Answer updated. – chqrlie Jan 23 '17 at 23:12
  • @John take a look here: http://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf – yano Jan 23 '17 at 23:18
1

Two things:

  1. scanf needs the address of the variables it is scanning for because it needs to write to those variables. printf does not; it takes the value of the arguments, since it is just reading/printing them, so the line:

    printf("The volume is %lf cubic inches.\n", &VolumeCubicInch);
    

    Should just be:

    printf("The volume is %lf cubic inches.\n", VolumeCubicInch);
    
  2. In order to print out "XXXXXXX.XX" (i.e. two decimal places) in your output as you specify in the comments, you can use the %.2 format length modifier, as in:

    printf("The volume is %.2f cubic inches.\n", VolumeCubicInch);
    
Govind Parmar
  • 18,500
  • 6
  • 49
  • 78