2017-01-23 131 views
-1

我做一个比较简单的程序来计算的长度×宽×高为立方英寸。我应该得到一个像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; 
} 
+5

这真的太糟糕了。不幸的是,没有看到你的代码,没有任何人可以帮助你。祝你好运。 –

+0

请使用迄今为止所做的工作。 –

+0

这不是编译器给你0.000000。这是你的程序。 – DyZ

回答

4

您传递的结果,而不是它的价值地址:

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

而应该传递值,并指定2位小数:

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

注:

  • l修改有必要scanf转换为double格式而不是floatprintf总是促进floatdouble,所以%f格式用于两者,并且如果指定的l被忽略。
  • 对于类型long double,您将在scanfprintf中使用%Lf转换说明符。
  • 您可以通过在%f格式字符之间的小数点后传递精度字段来指定小数位数。
+0

@凯文:你是对的。 'l'对'scanf()'是必须的,但如果被'printf'忽略。 – chqrlie

+0

谢谢你解决了它! – John

+0

谢谢你提到我正要问关于浮球还是双桨的说明。是否可以添加关于使用long float/long double的注释? – John

1

两件事情:

  1. scanf需要变量的地址是扫描,因为它需要写入这些变量。 printf不;它需要的参数的价值,因为它只是读/打印它们,因此该行:

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

    应该仅仅是:

    printf("The volume is %lf cubic inches.\n", VolumeCubicInch); 
    
  2. 为了打印出“XXXXXXX。 XX”(即保留两位小数),作为你在意见栏内注明您的输出,你可以使用%.2格式长度修改,如:

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

谢谢你解决了 – John