编译器不会将长度*宽度*高度计算为立方英寸

时间:2017-01-23 22:48:53

标签: c

我正在做一个相对简单的程序来计算长度*宽度*高度为立方英寸。我应该得到像XXXXXXX.XX这样的答案,但编译器会给我0.000000

我没有其他错误。我的主要问题是如何计算数字?

#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;
}

2 个答案:

答案 0 :(得分:4)

您传递结果的地址而不是其值:

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

您应该传递该值并指定2个小数位:

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

注意:

  • l修饰符是scanf转换为double格式而不是float所必需的。 printf始终将float值提升为double,因此%f格式用于两者,如果指定则忽略l
  • 对于long double类型,您可以使用%Lfscanf中的printf转换说明符。
  • 您可以通过在%f格式字符之间的小数点后面传递 precision 字段来指定小数位数。

答案 1 :(得分:1)

两件事:

  1. scanf需要地址正在扫描的变量,因为它需要写入这些变量。 printf没有;它需要参数,因为它只是读取/打印它们,所以行:

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

    应该是:

    printf("The volume is %lf cubic inches.\n", VolumeCubicInch);
    
  2. 为了打印&#34; XXXXXXX.XX&#34; (或者在注释中指定的输出中的两位小数),您可以使用%.2格式长度修饰符,如:

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