简体   繁体   English

C ++中的case / switch语句

[英]the case/switch statement in c++

I am trying to learn C++ and so far i have been on the right but i have been have a simple problem and i am getting confused on the case statement i have known how to deal with the if statement and i understand it very well,i working classes and methods in my code below which works perfectly i want to know how i can change this if statement and replace it with the case statement ,when i try it its not working but this is my code for the if statement how can i make it work if i want to use case instead of if ??thanks in advance 我正在尝试学习C ++,到目前为止我是对的,但是我一直有一个简单的问题,我对案例陈述感到困惑,我知道如何处理if陈述,并且我非常了解它,我我的代码下面的工作类和方法完美地工作,我想知道如何更改此if statement并用case statement替换,当我尝试不起作用时,这是if语句的代码,我该如何使如果我想使用用case而不是if提前感谢,它会工作

my code 我的密码

void SetType(){
        cout<<"Book SetType"<<endl;
        Choice:
        cout<<"Please Select from the list: \n 1- Technical literature \n 2- Fiction literature \n 3- Textbook"<<endl;
        int i;
        cin >> i;
        if ((i>0)&(i<=3)) {
            if (i==1) Type="Technical literature";
            if (i==2) Type="Fiction literature";
            if (i==3) Type="Textbook";
        }
        else 
        {
            cout << "Erorr you entered a wrong choice" << endl;
            goto Choice;
        }

    }
    switch(i) {
    case 1:
        Type="Technical literature";
        break;
    case 2:
        Type="Fiction literature";
        break;
    case 3:
        Type="Textbook";
        break;
    default:
        cout << "Erorr you entered a wrong choice" << endl;
        goto Choice;
    }

A switch / case is totally fit for your situation. switch / case完全适合您的情况。 Use a switch / case when you have discrete values you can test on a single variable, in your case i . 当您有离散值时,可以使用switch / case ,可以在单个变量i

It works by defining each case and a default one in case the variable doesn't match with any case. 它通过定义每种casedefault情况(如果变量与任何情况都不匹配)来工作。 Here's what your code would look like with a switch / case : 这是使用switch / case代码的样子:

switch(i)
{
    case 1:
        Type="Technical literature";
        break;
    case 2:
        Type="Fiction literature";
        break;
    case 3:
        Type="Textbook";
        break;
    default: 
        cout << "Erorr you entered a wrong choice" << endl;
        goto Choice;
}

break is used to prevent the code from one case to continue executing the code of the following case . break用于防止所述代码从一个case继续执行下面的代码case

I would strongly advise you learn better methods than using a goto to go back to your Choice selection. 与使用goto返回您的Choice选择相比,我强烈建议您学习更好的方法。

Here's a new version with a slightly better "input loop", without using goto 这是一个新版本,其“输入循环”略好一些,而无需使用goto

void SetType()
{
    cout << "Book SetType" << endl;
    bool validChoice;
    do
    {
        validChoice = true; // Invalidate it in case of wrong choice
        cout << "Please Select from the list: \n 1- Technical literature \n 2- Fiction literature \n 3- Textbook" << endl;
        int i;
        cin >> i;
        switch(i)
        {
        case 1:
            Type="Technical literature";
            break;
        case 2:
            Type="Fiction literature";
            break;
        case 3:
            Type="Textbook";
            break;
        default:
            cout << "Error you entered a wrong choice" << endl;
            validChoice = false;
            cin.clear();
            string dummyLine;
            getline(cin, dummyLine);
        }
    } while(validChoice == false);
}

I added some code to remove input that is not a number, otherwise cin will keep failing. 我添加了一些代码来删除不是数字的输入,否则cin将继续失败。

void SetType(){
        cout<<"Book SetType"<<endl;
        Choice:
        cout<<"Please Select from the list: \n 1- Technical literature \n 2- Fiction literature \n 3- Textbook"<<endl;
        int i;
        cin >> i;
        switch(i)
        {
            case 1:
            {
                Type="Technical literature";
                break;
            }
            case 2:
            {
                Type="Fiction literature";
                break;
            }
            case 3:
            {
                Type="Textbook";
                break;
            }
            default:
            {
                cout << "Erorr you entered a wrong choice" << endl;
                goto Choice;
                break;
            }
        }

    }
void SetType(){
    cout<<"Book SetType"<<endl;
Choice:
    cout<<"Please Select from the list: \n 1- Technical literature \n 2- Fiction literature \n 3- Textbook"<<endl;
    int i;
    cin >> i;
    switch(i) {
        case 1: Type="Technical literature"; break;
        case 2: Type="Fiction literature"; break;
        case 3: Type="Textbook"; break;
        default:
            cout << "Erorr you entered a wrong choice" << endl;
            goto Choice;
    }
}

Ps Many folks break into a cold sweat and have palpitations at the sight of a goto. 附言:许多人一看到goto便冒冷汗,心have。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM