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

C++ 如果switch语句达到默认值,则重复do while循环,c++,switch-statement,do-while,C++,Switch Statement,Do While,我有一个do-while循环,请求用户输入。在这个do-while循环中,我有一个switch语句。我怎样才能使它在满足默认值的情况下重复循环,再次询问用户的性别 do { cout << "What is your weight?" << endl; cin >> weight; cout << "What is your height?" << endl; ci

我有一个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');
do
{
重量;
库特高度;
年龄;
性别差异;
开关(性别)
{
案例“M”:
案例“m”:

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

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

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

这类问题的典型模式是循环,直到用户输入有效的输入。即

    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
do{
重量;
库特高度;
年龄;
性别差异;
}而(!validGender(性别));
//处理有效输入

虽然这并不能准确地回答您的问题,但这是一个很好的模式,适用于您正在尝试的操作。

由于
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');
do
{
再一次:
重量;
库特高度;
年龄;
性别差异;
开关(性别)
{
案例“M”:
案例“m”:
不能
do
{
重量;
库特高度;
年龄;
布尔重复(真);
做{
性别差异;
开关(性别)
{
案例“M”:
案例“m”:

你怎么做才能让你只问性别而不是每个问题?
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');