如何使用在C++中具有多个参数的if语句来调用多个函数
How do i call multiple functions using if statements that have multiple parameters in C++
这个程序为每个条件运行所有函数,而它应该为每个条件只运行一个函数。为什么?我应该编写计算球体、圆柱体和圆锥体的体积和表面积的函数:我不知道是if语句搞砸了,还是函数本身搞砸了。该程序的理想输出如下:
选择形状(1)球体(2)圆柱体(3)圆锥体(q)退出:1选择计算(1)体积(2)表面积:1输入球体半径:5.5球体体积为696.91
选择形状(1)球体(2)圆柱体(3)圆锥体(q)退出:1选择计算(1)体积(2)表面积:2输入球体半径:5.5球体表面积为380.133
选择形状(1)球体(2)圆柱体(3)圆锥体(q)退出:2选择计算(1)体积(2)表面积:1输入圆柱体半径:5.5输入圆柱体高度:4.2气瓶容积399.139
选择形状(1)球体(2)圆柱体(3)圆锥体(q)退出:2选择计算(1)体积(2)表面积:2输入圆柱体半径:5.5输入圆柱体高度:4.2气缸表面积为335.208
选择形状(1)球体(2)圆柱体(3)圆锥体(q)退出:3选择计算(1)体积(2)表面积:1输入圆锥体半径:5.5输入圆锥体高度:4.2锥体体积为133.046
选择形状(1)球体(2)圆柱体(3)圆锥体(q)退出:3选择计算(1)体积(2)表面积:2输入圆锥体半径:5.5输入圆锥体高度:4.2锥体表面积为214.607
选择形状(1)球体(2)圆柱体(3)圆锥体(q)退出:q
再见!
#include <iostream>
#include <math.h>
using namespace std;
double sphere_volume(double radius);
double sphere_surface_area(double radius);
double cylinder_volume(double radius, double height);
double cylinder_surface_area(double radius, double height);
double cone_volume(double radius, double height);
double cone_surface_area(double radius, double height);
int main()
{
double entHeight;
double entRadius;
char shapeCall;
char compCall;
cout << "Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: ";
cin >> shapeCall;
cout << "Select a Computation (1) volume (2) surface area: ";
cin >> compCall;
if ( shapeCall == 1 )
{
if ( compCall == 1)
cout << "Enter Radius: ";
cin >> entRadius;
cout << sphere_volume (entRadius) << endl;
}
if ( shapeCall == 1 )
{
if ( compCall == 2)
cout << "Enter Radius: ";
cin >> entRadius;
cout << sphere_surface_area (entRadius) << endl;
}
if ( shapeCall == 2 )
{
if ( compCall == 1)
cout << "Enter Radius: ";
cin >> entRadius;
cout << "Enter Height: ";
cin >> entHeight;
cout << cylinder_volume (entRadius, entHeight) << endl;
}
if (shapeCall == 2 )
{
if ( compCall == 2)
cout << "Enter Radius: ";
cin >> entRadius;
cout << "Enter Height: ";
cin >> entHeight;
cout << cylinder_surface_area (entRadius, entHeight) << endl;
}
if (shapeCall == 3 )
{
if ( compCall == 1)
cout << "Enter Radius: ";
cin >> entRadius;
cout << "Enter Height: ";
cin >> entHeight;
cout << cone_volume (entRadius, entHeight) << endl;
}
if (shapeCall == )
{
if ( compCall == 2)
cout << "Enter Radius: ";
cin >> entRadius;
cout << "Enter Height: ";
cin >> entHeight;
cout << cone_surface_area (entRadius, entHeight) << endl;
}
return 0;
}
double sphere_volume(double radius)
{
double sphereVolume;
sphereVolume = 3.14 * pow(radius, 3) * 4/3;
return sphereVolume;
}
double sphere_surface_area(double radius)
{
double sphereSurfArea = 4 * 3.14 * pow( radius, 2 );
return sphereSurfArea;
}
double cylinder_volume(double radius, double height)
{
double cylinderVolume = 3.14 * pow (radius, 2) * height;
return cylinderVolume;
}
double cylinder_surface_area(double radius, double height)
{
double cylinderSurfArea = 2 * 3.14 * pow (radius, 2) + 2 * 3.14 * radius * height;
return cylinderSurfArea;
}
double cone_volume(double radius, double height)
{
double coneVolume;
coneVolume = (1/3) * 3.14 * pow (radius, 2) * height;
return coneVolume;
}
double cone_surface_area(double radius, double height)
{
double coneSurfArea = 3.14 * pow (radius, 2) + 3.14 * sqrt(pow (radius, 2) + pow (height, 2));
return coneSurfArea;
}
if语句期望采用以下格式
if (expression)
statement;
在expression
评估为非零数字或true
的情况下,执行statement
。在其他所有事件中,情况并非如此。
它之所以显示为,就好像每个条件的计算结果都为true,是因为您在整个代码中更改了if语句的格式。你似乎经常使用以下格式作为条件句。
if ( compCall == 1)
cout << "Enter Radius: ";
cin >> entRadius;
cout << sphere_volume (entRadius) << endl;
请考虑if语句将仅与直接在后面的语句相关联。因此,该代码相当于
if ( compCall == 1)
{
cout << "Enter Radius: ";
}
cin >> entRadius;
cout << sphere_volume (entRadius) << endl;
你遇到的问题是因为没有正确地用大括号括住你的条件句。它可能仍然被认为是有效的代码,但如果使用不当,可能会产生非常意外的结果。在这种情况下,下面的代码将产生您期望的结果,因为与条件相关的语句正确地用大括号括起来。
if ( compCall == 1)
{
cout << "Enter Radius: ";
cin >> entRadius;
cout << sphere_volume (entRadius) << endl;
}
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复