gpt4 book ai didi

c++ - 运行时检查失败 #3 - 变量 'sBucketH' 未经初始化就被使用

转载 作者:行者123 更新时间:2023-11-28 06:38:37 25 4
gpt4 key购买 nike

我还不知道为什么会抛出运行时错误!我将允许用户输入一个小桶和一个大桶的直径和高度,并在较小的桶放在较大的桶内时计算两个桶之间的体积。

#include <iostream>

using namespace std;

int main()
{
//Declaring Variables

const double Pi= 3.14;
double lBucketDiameter(0);
double sBucketDiameter(0), sBucketH(0), lBucketH(0);
double volume;
double lBucketVolume, sBucketVolume;
double lBucketRadius;
double sBucketRadius;
//Output and Input for LARGE BUCKET

cout << "Please enter the Large Buckets Diameter: " << endl;
cin >> lBucketDiameter;

cout << "Please enter the Large Buckets Height: " << endl;
cin >> lBucketH;

//Output and Input for Small Bucket

cout << "Please enter the Small Buckets Diameter: " << endl;
cin >> sBucketDiameter ;

cout << "Please enter the Small Buckets Height: " << endl;
cin >> sBucketH ;

//Formula for Radius

lBucketRadius = lBucketDiameter / 2 ;
sBucketRadius = sBucketDiameter / 2 ;


//Formula for Volume sum for Large and Small Buckets

lBucketVolume = Pi * (lBucketRadius * lBucketRadius) * lBucketH ;

sBucketVolume = Pi * (sBucketRadius * sBucketRadius) * sBucketH ;

//Subtracting to find the final answer.

volume = lBucketVolume - sBucketVolume;

//Outputting result

cout << "The volume of space left between the Buckets is: " << volume << "cm3" << endl;

system("pause");

return 0;
}

最佳答案

警告是绝对正确的。

您需要在输入后处理这些值。现在,您甚至在用户输入之前就使用随机值进行计算。这样,您将得到垃圾。

在用户输入后移动您的计算。

关于c++ - 运行时检查失败 #3 - 变量 'sBucketH' 未经初始化就被使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26356753/

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