4

我有一个 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');
4

4 回答 4

5

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

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

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

于 2013-09-18T18:50:03.127 回答
4

这种事情的典型模式是循环直到用户输入有效输入。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

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

于 2013-09-18T18:52:22.440 回答
1
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');
于 2013-09-18T19:04:42.260 回答
1

由于重载break意味着“开关盒结束”和“退出循环”,这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');

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

于 2013-09-18T18:57:56.377 回答