否则if在请求用户输入之前自动执行

else if executes executes automatically before asking for user input

本文关键字:执行 输入 if 请求 用户 否则      更新时间:2023-10-16

我是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.