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

时间:2015-06-29 08:37:36

标签: c++

我是c ++编程的新手,我想创建一个程序来查找区域,我使用switch case选择形状的形状体积,如果在switch内部选择区域或体积,则使用else,但它只执行一个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 correctly\n";
    cout << "1-square\n";
    cout << "2-Cube\n";
    cout << "3-Cuboid\n";
    cout << "4-Circle\n";
    cout << "5-Sphere\n";
    cout << "6-rectangle\n";

    cout << "Select any shape by typing the no correctly\n";
    cin >> userchoice;

    switch (userchoice) {
        case '1':
            cout << "3-area\n";
            cout << "4-perimeter\n";
            if (userchoice = 3) {
                cout << "Enter side a\n";
                cin >> a;
                cout << "Area of square is " << a * a << "sq.units" << endl;
            } else if (userchoice = 4) {
                cout << "Enter side\n";
                cin >> a;
                cout << "Perimeter of square is " << 4 * a << "sq.units" << endl;
            }
            break;
        case '2':
            cout << "1-area\n";
            cout << "2-volume\n";
            if (userchoice = 1) {
                cout << "Enter side a\n";
                cin >> a;
                cout << "Area of cube is " << 6 * a * a << "sq.units" << endl;
            } else if (userchoice = 2) {
                cout << "Enter side a\n";
                cin >> a;
                cout << "Volume of cube is " << a * a * a << "cu.units" << endl;
            }
            break;
        case '3':
            cout << "Enter length, breadth, height\n";
            cin >> breadth;
            cin >> length;
            cin >> height;
            cout << "Area of cuboid is " << (length * breadth * height) << "sq.units" << endl;
            break;
        case '4':
            cout << "1-circumference\n";
            cout << "2-Area\n";
            if (userchoice = 1) {
                cout << "Enter the radius of circle\n";
                cin >> radius;
                cout << "Circumference of circle is " << 2 * PI * radius << endl;
            } else if (userchoice = 2) {
                cout << "Enter the radius of circle\n";
                cin >> radius;
                cout << "Area of circle is " << PI * radius * radius << "sq.units" << endl;
            }
            break;
        case '5':
            cout << "1-Area\n";
            cout << "2-volume\n";
            if (userchoice = 1) {
                cout << "Enter the radius of Sphere\n";
                cin >> radius;
                cout << "Area of Sphere is " << 4 * PI * radius * radius << "sq.units" << endl;
            } else if (userchoice = 2) {
                cout << "Enter the radius of Sphere\n";
                cin >> radius;
                cout << "Volume of Sphere is " << (4 / 3 * PI * radius * radius) << "cu.units" << endl;
            }
            break;
        case '6':
            cout << "1-Area\n";
            cout << "2-perimeter\n";
            if (userchoice = 1) {
                cout << "Enter length width\n";
                cin >> length;
                cin >> width;
                cout << "Area of rectangle: " << length * width << "sq.units" << endl;
            } else if (userchoice = 2) {
                cout << "Enter side\n";
                cin >> a;
                cout << "Perimeter of rectangle is " << 4 * a << "sq.units" << endl;
            }
            break;
    }
}

1 个答案:

答案 0 :(得分:1)

切换语句只能通过一个案例执行。它可以&#34;通过&#34;如果你不使用休息时间。如果您的意思是它没有执行开关语句中的任何 if 语句,那么因为您正在使用&# 34;分配&#34;条件中的运算符(=)。你应该使用&#34;等于&#34;运算符(==)代替。

所以 if 语句应该是这样的:

if (userchoice == 1) {
    // ... do something ...
}
else if (userchoice == 2) {
    // ... do something else ...
}

即使你这样做,你还有另一个我注意到的问题。您在 if 开关语句的条件下使用相同的变量。这样做将使得无法执行某些路线。您应该为两者使用单独的变量,并提示用户同时使用两者。要么那么,要么重新分配&#34; userchoice&#34;用于 if 语句。

此外,您还要声明&#34; userchoice&#34;作为一个炭。如果将其声明为char,则应在条件中测试char。如果不是,请将其声明为int。