一旦用户错了两次,就结束循环

Ending a loop once the user is wrong twice?

本文关键字:两次 结束 循环 用户 错了      更新时间:2023-10-16

我正在做一个测试和测试用户。如果用户错了,他可以有第二次机会或跳过,如果他选择了第二次,但又错了,游戏就结束了。我该如何打破这个循环来结束游戏?我试着做了一个循环,

do { stuff} while (wrong<2) while counting ++wrong;

每次他都错了,但没有奏效。

我在下面用//语句标记了++wrong

void player_try (string questions[][5], char answers[])
{
    char user_guess;
    int m = 0;
    srand(time(NULL));
    int x;
    int choice;
    int wrong =0;
    for (m=0; m<7; m++)
    {
        do
        {
            x = (rand() % 7);
            cout << user_name << ": Here is question number " << m+1 << endl;
            cout << m+1 << ". " << questions[x][0]<< endl;
            cout << "A. " << questions[x][1]<< endl;
            cout << "B. " << questions[x][2]<< endl;
            cout << "C. " << questions[x][3]<< endl;
            cout << "D. " << questions[x][4]<< endl;
            cin >> user_guess;
            user_guess = toupper(user_guess);
            while (!(user_guess >= 'A' && user_guess <= 'D'))
            {
                cout << "Please choose a valid answer.";
                cin>> user_guess;
            }
            if (user_guess != answers[x])
            {
                cout <<"Wrong!" <<endl;     
                ++wrong;    // THIS IS WHERE I COUNT WRONG ONCE
                cout << "Skip this question or take a chance at greatness?"      << endl;
                cout << "Press 1 to skip, press 2 to take a chance at greatness" << endl;
                cin  >> choice;
                if (choice == '1')
                {
                    cout << "we shall skip this question." << endl;
                }
                else
                {
                    cout << "I applaud your bravery." << endl;
                    cout << user_name << ": Here is question number " << m+1 << endl;
                    cout << m+1 << ". " << questions[x][0]<< endl;
                    cout << "A. " << questions[x][1]<< endl;
                    cout << "B. " << questions[x][2]<< endl;
                    cout << "C. " << questions[x][3]<< endl;
                    cout << "D. " << questions[x][4]<< endl;
                    cin >> user_guess;
                    user_guess = toupper(user_guess);
                    while (!(user_guess >= 'A' && user_guess <= 'D'))
                    {
                        cout << "Please choose a valid answer.";
                        cin>> user_guess;
                    }
                }
                if (toupper(user_guess) != answers[x])
                {
                    cout <<"Wrong!" <<endl;
                    ++wrong;;  // THIS IS WHERE I CANT WRONG TWICE
                }
                else
                {
                    cout << "correct!" << endl;
                }
            }
            else
            {
                cout << "correct!" << endl;
            }
        }
        while(wrong < 2);
    }
}

将函数返回类型更改为整数。这仅仅意味着将"void"改为"int"然后,在函数内部,将return 0;放置在希望函数终止的点上。确保在用户也获胜的情况下包含另一个return 1;

main()函数就是这样工作的。考虑:

int main() 
{
  string tester = "some string";
  if(tester == "some string")
    return 1;
  cout << "Hey!"
  return 0;
}

在上述情况下,main()终止于"return 1;",因为if语句为TRUE。请注意,"嘿!"永远不会打印出来。它将以同样的方式为您的功能工作。

另外,您可以使用该返回值让OTHER函数(如main())知道函数终止是因为用户赢了(返回1)还是输了(返回0)。

是的,break语句也是终止循环的有效方法,但我认为这种方法是更安全、更干净的方法。通常,我们想知道函数或程序是否成功。

您可以使用break;如果该人两次答错了答案,则进行陈述。

根据意见。你可以放弃do-wile循环,而选择one-for-loop。如果错误的猜测是2

,只需在底部放一个断点

对于重构代码以消除重复工作,有几个很好的建议,但要使程序立即运行,必须突破围绕do { } while(wrong < 2)循环的for循环。

一个简单的方法是修改for循环来测试错误的变量。额外的好处是,如果我正确阅读了所有内容,您就不再需要do{ } while();循环。

for (m=0; m<7 && wrong < 2; m++)