C++ 如果 switch 语句达到默认值,则重复执行 while 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18880021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:17:25  来源:igfitidea点击:

repeat do while loop if switch statement reaches default

c++switch-statementdo-while

提问by Mr. 1.0

I have a do while loop asking for user input. Inside this do while loop I have a switch statement. How can I make it so that if the default value is met repeat the loop asking for the users gender again?

我有一个 do while 循环要求用户输入。在这个 do while 循环中,我有一个 switch 语句。我怎样才能让它在满足默认值的情况下再次重复询问用户性别的循环?

do
    {
        cout << "What is your weight?" << endl;
        cin >> weight;
        cout << "What is your height?" << endl;
        cin >> height;
        cout << "What is your age?" << endl;
        cin >> age;
        cout << "What is your gender?" << endl;
        cin >> gender;

        switch (gender)
        {
            case 'M':
            case 'm':
                cout << endl << Male(weight, height, age);
                break;
            case 'F':
            case 'f':
                cout << endl << Female(weight, height, age);
                break;
            default:
                cout << "What is your gender?";
        }

        cout << "Do you want to continue? (Y/N)" << endl;
        cin >> stopApp;

    } while(toupper(stopApp) == 'Y');

回答by pippin1289

One option is to set up a boolean value and if the default case is reached set it to true to repeat.

一种选择是设置一个布尔值,如果达到默认情况,则将其设置为 true 以重复。

bool repeat;
do {
  repeat = false;
  //switch statement
  switch {
    default:
      repeat = true;
  }
while(repeat);

You could appropriately use repeat to know which question you would like to repeat as well.

您可以适当地使用重复来知道您还想重复哪个问题。

回答by Doug T.

A typical pattern for this kind of thing is to loop until the user enters valid input. IE

这种事情的典型模式是循环直到用户输入有效输入。IE

    do {
        cout << "What is your weight?" << endl;
        cin >> weight;
        cout << "What is your height?" << endl;
        cin >> height;
        cout << "What is your age?" << endl;
        cin >> age;
        cout << "What is your gender?" << endl;
        cin >> gender;
    } while (!validGender(gender));

    // process valid input

While this doesn't precisely answer your question, it is a good pattern for what you're trying to do.

虽然这并不能准确回答您的问题,但它是您尝试做的事情的一个很好的模式。

回答by zwol

Thanks to the overloading of breakto mean both "end of switch case" and "exit loop", this is one of those unusual times where gotois appropriate.

由于重载breakto 意味着“开关案例结束”和“退出循环”,这goto是适当的不寻常时期之一。

do
    {
    again:
        cout << "What is your weight?" << endl;
        cin >> weight;
        cout << "What is your height?" << endl;
        cin >> height;
        cout << "What is your age?" << endl;
        cin >> age;
        cout << "What is your gender?" << endl;
        cin >> gender;

        switch (gender)
        {
            case 'M':
            case 'm':
                cout << endl << Male(weight, height, age);
                break;
            case 'F':
            case 'f':
                cout << endl << Female(weight, height, age);
                break;
            default:
                cout << "What is your gender?";
                goto again;
        }

        cout << "Do you want to continue? (Y/N)" << endl;
        cin >> stopApp;

    } while(toupper(stopApp) == 'Y');

FYI "male" and "female" are not the only options for gender. Depending on your larger goals, I would recommend that you either avoid asking this question at all, allow the user to provide an arbitrary phrase in response, or, if this is a medical application where biological sexis the actual relevant piece of information, ask for that instead (and again allow provision of an arbitrary phrase, because that'snot binary either).

仅供参考,“男”和“女”不是性别的唯一选择。根据你更大的目标,我建议你要么完全避免问这个问题,允许用户提供一个任意的短语作为回应,或者,如果这是一个医学应用,其中生物性别是实际相关的信息,问为此(并再次允许提供任意短语,因为也不是二进制的)。

回答by Vadim Prozorov

do
{
    cout << "What is your weight?" << endl;
    cin >> weight;
    cout << "What is your height?" << endl;
    cin >> height;
    cout << "What is your age?" << endl;
    cin >> age;

    bool repeat(true);
    do {
        cout << "What is your gender?" << endl;
        cin >> gender;

        switch (gender)
        {
            case 'M':
            case 'm':
                cout << endl << Male(weight, height, age);
                repeat = false;
                break;
            case 'F':
            case 'f':
                cout << endl << Female(weight, height, age);
                repeat = false;
                break;
            default:
                break;
        }
    } while(repeat)

    cout << "Do you want to continue? (Y/N)" << endl;
    cin >> stopApp;

} while(toupper(stopApp) == 'Y');