【发布时间】:2023-03-07 19:14:01
【问题描述】:
以下代码要求用户选择一个形状,输入该形状的尺寸,并显示其体积。
当我运行代码时,我得到以下输出,显示结果不是数字(NaN):
我意识到这一定与我的变量是局部变量而不是全局变量以及以下函数调用有关:
choice = ReadInputShapeChoice();
readshapedimension(choice);
result = CalculateBasicVolume(choice);
没有传递所需的数据。已尝试解决此问题并将变量放在正确的位置但无济于事,我是使用任何形式的编码语言的初学者,发现整个程序的创建相当具有挑战性。
我已经发布了一个类似的问题,但是这个问题要详细得多,如果有人能阐明我如何修改变量的位置以使程序正确运行,我将不胜感激。谢谢。
完整代码为:
#include <iostream>
using namespace std;
double height, width, length, radius, base_area, result;
//Function prototypes
int ReadInputShapeChoice();
void readshapedimension(int choice);
float CalculateBasicVolume(int choice);
void PrintResult(int choice);
double rectangular_solid(double length1, double width1, double height1);
double cylinder(double radius2, double height2);
double cone(double radius3, double height3);
double sphere(double radius4);
double square_based_pyramid(double height5, double base_area5);
//function definitions
double rectangular_solid(double length1, double width1, double height1)
{
double value;
value = (length1 * width1 * height1);
return value;
}
double cylinder(double radius2, double height2)
{
double value;
value = (3.14159 * (radius2 * radius2) * height2);
return value;
}
double cone(double radius3, double height3)
{
double value;
value = ((3.14159 * (radius3 * radius3) * height3) / 3);
return value;
}
double sphere(double radius4)
{
double value;
value = ((3.14159 * (radius4 * radius4 * radius4))*(4 / 3));
return value;
}
double square_based_pyramid(double height5, double base_area5)
{
double value;
value = ((height5 * base_area5) * (1 / 3));
return value;
}
int ReadInputShapeChoice()
{ int choice;
cout << "Choose what shape you want to calculate" << endl;
cout << "1 = Rectangular solid" << endl;
cout << "2 = Cylinder" << endl;
cout << "3 = Cone" << endl;
cout << "4 = Sphere" << endl;
cout << "5 = Square based pyramid" << endl;
cin >> choice;
return choice;
}
void readshapedimension(int choice)
{
switch (choice)
{
case 1:
{
int length, width, height;
cout << "You have chosen rectuangular solid" << endl;
cout << "Enter the values for length width and height" << endl;
cin >> length >> width >> height;
break;
}
case 2:
{
int radius, height;
cout << "You have chosen cylinder" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 3:
{
int radius, height;
cout << "You have chosen cone" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 4:
{
int radius;
cout << "You have chosen sphere" << endl;
cout << "Enter the radius" << endl;
cin >> radius;
break;
}
case 5:
{
int height, base_area;
cout << "You have chosen square based pyramid" << endl;
cout << "Enter height and area of the base" << endl;
cin >> height >> base_area;
break;
}
}
}
float CalculateBasicVolume(int choice)
{
switch (choice)
{
int result;
case 1:
{
result = rectangular_solid(length, width, height);
break;
}
case 2:
{
result = cylinder(radius, height);
break;
}
case 3:
{
result = cone(radius, height);
break;
}
case 4:
{
result = sphere(radius);
break;
}
case 5:
{
result = square_based_pyramid(height, base_area);
break;
}
return result;
}
}
void PrintResult(int choice)
{
switch (choice)
{
case 1:
{
cout << "The volume of the rectangular solid is " << result << endl;
break;
}
case 2:
{
cout << "the volume of the cylinder is " << result << endl;
break;
}
case 3:
{
cout << "The volume of the cone is " << result << endl;
break;
}
case 4:
{
cout << "The volume of the sphere is " << result << endl;
break;
}
case 5:
{
cout << "the volume of the square based pyramid is " << result << endl;
break;
}
}
}
int main() {
int choice;
choice = ReadInputShapeChoice();
readshapedimension(choice);
result = CalculateBasicVolume(choice);
PrintResult(choice);
return 0;
}}
【问题讨论】:
-
在您的案例块中,当块超出范围时,您使用 cin 读取的变量将不存在。
-
我意识到这一定与我的变量是局部变量而不是全局变量有关,问题是您定义它们的范围很窄. 不要在你的交换机中声明这些。在 switch 语句之外获取所有声明。还可以在需要时摆脱全局变量并传递变量。
-
您会收到运行时错误,因为您最终使用了从未初始化过的全局变量。它们与 case 语句中的相同命名变量没有关系或联系。
-
好的,我已经删除了函数定义 void readshapeimension(int choice) 中包含的变量声明。所以现在它们只在顶部声明,所以我假设它们现在是全球性的。我仍然得到与打印屏幕中显示的结果相同的结果,我还需要做什么才能使其正常工作?就像我说的,非常感谢任何帮助,因为我已经坚持了好几天了,它真的占用了我的日程安排。
-
这是多态的工作。
标签: c++ visual-studio visual-studio-2015 runtime-error