否则if在请求用户输入之前自动执行
else if executes executes automatically before asking for user input
我是c++编程的新手,我想创建一个程序来查找形状的面积和体积。我使用switch case来选择形状,并在switch内部使用else-if来选择面积或体积,但它只执行一个switch语句。
#include <iostream>
#include<cmath>
#define _USE_MATH_DEFINES
#include<math.h>
#define PI 3.14159
using namespace std;
int main() {
double radius;
double square;
double qube;
double quboid;
double cylinder;
double sphere;
double a;
double length;
double breadth;
double height;
double width;
char userchoice1;
char userchoice;
cout << "Select any shape by typing the no correctlyn";
cout << "1-squaren";
cout << "2-Cuben";
cout << "3-Cuboidn";
cout << "4-Circlen";
cout << "5-Spheren";
cout << "6-rectanglen";
cout << "Select any shape by typing the no correctlyn";
cin >> userchoice;
switch (userchoice) {
case '1':
cout << "3-arean";
cout << "4-perimetern";
if (userchoice = 3) {
cout << "Enter side an";
cin >> a;
cout << "Area of square is " << a * a << "sq.units" << endl;
} else if (userchoice = 4) {
cout << "Enter siden";
cin >> a;
cout << "Perimeter of square is " << 4 * a << "sq.units" << endl;
}
break;
case '2':
cout << "1-arean";
cout << "2-volumen";
if (userchoice = 1) {
cout << "Enter side an";
cin >> a;
cout << "Area of cube is " << 6 * a * a << "sq.units" << endl;
} else if (userchoice = 2) {
cout << "Enter side an";
cin >> a;
cout << "Volume of cube is " << a * a * a << "cu.units" << endl;
}
break;
case '3':
cout << "Enter length, breadth, heightn";
cin >> breadth;
cin >> length;
cin >> height;
cout << "Area of cuboid is " << (length * breadth * height) << "sq.units" << endl;
break;
case '4':
cout << "1-circumferencen";
cout << "2-Arean";
if (userchoice = 1) {
cout << "Enter the radius of circlen";
cin >> radius;
cout << "Circumference of circle is " << 2 * PI * radius << endl;
} else if (userchoice = 2) {
cout << "Enter the radius of circlen";
cin >> radius;
cout << "Area of circle is " << PI * radius * radius << "sq.units" << endl;
}
break;
case '5':
cout << "1-Arean";
cout << "2-volumen";
if (userchoice = 1) {
cout << "Enter the radius of Spheren";
cin >> radius;
cout << "Area of Sphere is " << 4 * PI * radius * radius << "sq.units" << endl;
} else if (userchoice = 2) {
cout << "Enter the radius of Spheren";
cin >> radius;
cout << "Volume of Sphere is " << (4 / 3 * PI * radius * radius) << "cu.units" << endl;
}
break;
case '6':
cout << "1-Arean";
cout << "2-perimetern";
if (userchoice = 1) {
cout << "Enter length widthn";
cin >> length;
cin >> width;
cout << "Area of rectangle: " << length * width << "sq.units" << endl;
} else if (userchoice = 2) {
cout << "Enter siden";
cin >> a;
cout << "Perimeter of rectangle is " << 4 * a << "sq.units" << endl;
}
break;
}
}
开关语句只能通过一个事例执行。如果你不使用休息时间,它可能会"失败"。如果您的意思是它没有执行switch语句中的If任何语句,那是因为您在条件中使用了"assign"运算符(=)。您应该使用"等于"运算符(==)。
所以你的if语句应该是这样的:
if (userchoice == 1) {
// ... do something ...
}
else if (userchoice == 2) {
// ... do something else ...
}
即使你这样做,我也注意到你还有另一个问题。在if和开关语句的条件下使用相同的变量。这样做会使某些路由无法执行。您应该为两者使用单独的变量,并提示用户同时使用这两个变量。要么这样,要么重新分配if语句中要使用的"userchoice"。
另外,您将"userchoice"声明为一个字符。如果您将其声明为char,则应该在条件中测试char。如果不是,则将其声明为int.
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- C++如何在多次执行后记住过去给出的输入
- 是否可以在命令行中将输入参数传递给可执行文件
- 代码在执行之前终止,并且没有从用户那里获取输入
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 在"0"输入停止循环之前正确执行 while 循环?
- 提供变量作为 MATLAB 系统命令的输入参数,以便C++可执行文件
- 程序的执行在 C++ 中输入 char 变量后结束
- 我怎样才能接受输入作为双精度值并执行 IF ELSE 语句
- 为什么在代码块中执行时会跳过输入用户输入的行?
- 即使可执行文件的输入是从文件重定向的,如何强制从终端输入?
- 执行输入检查并在C++中初始化对象
- 在执行时进行输入验证
- 如何在 GLFW 中为游戏引擎执行正确的输入类
- 虽然循环在执行名称输入时过早退出
- getch() 函数在输入 key 时不执行任何操作
- 如何从Windows CMD运行可执行文件并将其传递给文件输入的参数参数和重定向
- 如何使用我的 if-else 语句根据用户权重的输入执行特定操作,然后将其显示到控制台?
- 确定QProcess是否已完成执行或正在等待用户输入(或响应)
- 我想知道如何在 C# 中执行输入和输出操作