C++ 想知道如何在VS2015运行时绕过奇怪的结果吗
以下代码要求用户选择形状,输入所述形状的尺寸,并显示其体积 它主要用于变量声明和函数调用,因为这是必需的 当我运行代码时,我得到以下输出: 我不明白为什么-nan(ind)出现而不是结果 以下是完整的代码:C++ 想知道如何在VS2015运行时绕过奇怪的结果吗,c++,visual-studio,visual-studio-2015,runtime,runtime-error,C++,Visual Studio,Visual Studio 2015,Runtime,Runtime Error,以下代码要求用户选择形状,输入所述形状的尺寸,并显示其体积 它主要用于变量声明和函数调用,因为这是必需的 当我运行代码时,我得到以下输出: 我不明白为什么-nan(ind)出现而不是结果 以下是完整的代码: #include <iostream> using namespace std; double height, width, length, radius, base_area, result; //Function prototypes int ReadInputSh
#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;
}
#包括
使用名称空间std;
双倍高度、宽度、长度、半径、底面积、结果;
//功能原型
int ReadInputShapeChoice();
void readshapedimension(int选择);
浮点计算体积(整数选择);
无效打印结果(整数选择);
双矩形实体(双长1,双宽1,双高1);
双气缸(双半径2,双高度2);
双锥面(双半径3,双高度3);
双球面(双半径4);
双方形金字塔(双高5,双基5);
//函数定义
双矩形实体(双长1,双宽1,双高1)
{
双重价值;
值=(长度1*宽度1*高度1);
返回值;
}
双油缸(双半径2,双高度2)
{
双重价值;
值=(3.14159*(半径2*半径2)*高度2);
返回值;
}
双锥面(双半径3,双高度3)
{
双重价值;
值=((3.14159*(半径3*半径3)*高度3)/3);
返回值;
}
双球面(双半径4)
{
双重价值;
值=((3.14159*(半径4*半径4*半径4))*(4/3));
返回值;
}
双方形金字塔(双高5,双基5)
{
双重价值;
值=((高度5*基础面积5)*(1/3));
返回值;
}
int ReadInputShapeChoice()
{int选择;
cout您正在将半径和高度读取到switch语句中声明的局部变量中。计算中使用的全局变量从未设置过,它们的默认值为0。您的计算结果为0.0/0.0,这将导致NaN(不是数字).谢谢你指出,我该如何将它们转换为全局变量?听起来可能很傻,但我认为我在代码开始时就已经这样做了。@FlewittConnor你做了,但是在案例3
下还有int radius,height
,它创建了一些具有相同名称和不同类型的局部变量。你知道吗'正在将输入读入局部变量,而不是全局变量。要解决此问题,只需删除readshapedimension
中case
语句中的变量声明即可。