gpt4 book ai didi

c - *需要帮助解决我的程序* Visual Studio 调试错误

转载 作者:行者123 更新时间:2023-11-30 21:29:08 24 4
gpt4 key购买 nike

输出带有调试错误,内容如下:

Runtime check failure #3 - The variable 'GPE2' is being used without being initialized.

我输入了物体的质量和高度:5kg,5m对于 GPE15kg,6m对于 GPE2 .

#include <stdio.h>
#define g 9.81

void main(void)
{
// step 1: variable declaration
float GPE1, GPE2, h;
int m;
// step 2: input
printf("Mr Lim: The higher the object, the greater the Gravitational Potential Energy. \n");
printf("Let me prove it to you with this simple physics question! \n\n");
printf("An object is located at the surface of the earth. \n");
printf("Calculate the Gravitational Potential Energy (in J) the body possesses. \n\n\n");
printf("Enter the mass of the body (in kg): ");
scanf("%d", &m);
printf("Enter the height of the body (in m): ");
scanf("%f", &h);

if (h > 0) {
// step 3: calculation
GPE1 = m * g * h;
// step 4: output
printf("The Gravitational Potential Energy, GPE (in J) = %.2f joules. \n\n", GPE1);
}
else {
printf("The Gravitational Potential Energy is undefined! \n");
}

printf("Mr Lim: Now, to prove my statement is valid \n");
printf("Find the GPE a body possesses, when its height is greater. \n");
printf("Enter the mass of the body (in kg): ");
scanf("%d", &m);
printf("Enter the height of the body (in m): ");
scanf("%f", &h);

if (GPE2 > GPE1) {
GPE2 = m * g * h;
printf("The Gravitational Potential Energy, GPE (in J) = %.2f joules. \n\n", GPE2);
printf("Mr Lim: See class, I told you! \n");
}
else {
printf("Class is not impressed. \n");
printf("Mr Lim should retire! \n");
}
}

最佳答案

错误陈述是正确的。您正在 if 语句中检查是否 GPE2 > GPE1,然后初始化 GPE2。

您可以在 if 语句上方添加一条语句,表示 GPE2 = 0;或者在程序顶部声明它时对其进行初始化。现在它保存着之前内存位置发生的任何垃圾。

关于c - *需要帮助解决我的程序* Visual Studio 调试错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33973261/

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