一旦用户错误两次结束循环?

时间:2012-02-10 01:44:44

标签: c++ loops count while-loop

我正在进行测验并测试用户。如果用户错了,他被允许第二次机会或跳过,如果他选择第二次机会并且再次出错,则游戏结束。如何突破这个循环以结束游戏?我尝试了do while循环,

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);
    }
}

3 个答案:

答案 0 :(得分: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语句也是终止循环的有效方法,但我认为这种方法是更安全,更清洁的方法。通常,我们想知道函数或程序是否成功。

答案 1 :(得分:1)

你可以休息一下;声明如果该人错误地得到了两次答案。

根据评论。你可以摆脱do while循环,转而使用for循环。如果错误猜测是2,请在底部休息一下

答案 2 :(得分:1)

有一些很好的建议可以重构代码以消除重复工作,但为了让程序立即运行,您必须突破for周围的do { } while(wrong < 2)循环循环。

执行此操作的一种简单方法是修改for循环以测试错误的变量。额外的好处是,如果我正确地阅读所有内容,您将不再需要do{ } while();循环。

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