gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:24:19 27 4
gpt4 key购买 nike

我正在做一个相对简单的程序来计算长*宽*高为立方英寸。我应该得到像 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;
}

最佳答案

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

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 类型,您可以在 scanfprintf 中使用 %Lf 转换说明符。
  • 您可以通过在 %f 格式字符之间的小数点后传递精度字段来指定小数位数。

关于编译器不会将长*宽*高计算为立方英寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41817083/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com