c ++中的case / switch语句

时间:2012-11-16 22:05:35

标签: c++

我正在努力学习C ++,到目前为止我一直在正确但我一直有一个简单的问题,我对案例陈述感到困惑,我知道如何处理if语句,我非常理解好吧,我在下面的代码中工作的类和方法完美无缺,我想知道如何更改if statement并将其替换为case statement,当我尝试它不起作用但这是我的if语句的代码如果我想使用case代替if,我该如何才能使其工作?提前感谢

我的代码

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;
        }

    }

4 个答案:

答案 0 :(得分:4)

    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;
    }

答案 1 :(得分:2)

switch / case完全适合您的情况。如果您有可以在单个变量上测试的离散值,请使用switch / case i

它的工作原理是定义每个casedefault,以防变量与任何情况不匹配。以下是使用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用于阻止一个case的代码继续执行以下case的代码。

我强烈建议您学习比使用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);
}

我添加了一些代码来删除不是数字的输入,否则cin将继续失败。

答案 2 :(得分:0)

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;
            }
        }

    }

答案 3 :(得分:0)

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;
    }
}

P.S。很多人都会出现冷汗,并且在看到goto的时候会出现心悸。