虽然循环没有要求输入

时间:2016-04-20 14:51:03

标签: c++

嗨,我刚刚在我的sem休息时进行编码,现在练习写了一个简单的奇数或者甚至是赌注,所以我做了一个小UI如下所示

int Option()
{
    int UI;
    bool Conformation = 0;
    while (Conformation == 0)
    {
    cout << "For odd enter 1 , for even enter 2\n Insert : ";
    cin >> UI;
    Conformation = 0;
        switch (UI)
        {
        case 1:
            cout << "You have selected odd \n are you sure?\n 1.Yes 2.NO \n Insert :";
            cin >> Conformation;
            break;
        case 2:
            cout << "You have selected even \n are you sure? 1.Yes 2.NO \n Insert :";
            cin >> Conformation;
            break;
        default:
            cout << "Invalid entery please enter another value\n";
            Conformation = 0;
            break;
        }
    }
    return UI;
}

我希望它循环通过第8行“cin&gt;&gt; UI;”当Conformation = 0时,它只是跳过任何形式的“cin”

提前谢谢。

1 个答案:

答案 0 :(得分:0)

你有:

cout << "You have selected odd \n are you sure?\n 1.Yes 2.NO \n Insert :";
cin >> Conformation;

由于1类型为2,因此ConfirmationConfirmationbool的使用极有可能使您失望。

我建议将Confirmation的类型更改为int