一旦用户错误两次结束循环? [英] Ending a loop once the user is wrong twice?

查看:111
本文介绍了一旦用户错误两次结束循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am doing a quiz and testing the user. If the user is wrong he is allowed a second chance or skip, if he chooses 2nd chance and is wrong again, the game is over. How do I break out of this loop to end the game? I tried a do while loop,

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

每次他错了,

我用 // ++错误 >

I have labeled the ++wrong with // statements below

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;

Change your function return type to an integer. That simply means changing "void" to "int." Then, inside the function place a return 0; at the point you want your function to terminate. Be sure you include another return 1; for the case that the user wins too.

()函数工作。请考虑:

This is how the main() function works. Consider:

int main() 
{
  string tester = "some string";
  if(tester == "some string")
    return 1;

  cout << "Hey!"
  return 0;
}



在上述情况下,main()终止于return 1;因为if语句是TRUE。注意嘿!从不打印。

In the above case, main() terminates at the "return 1;" because the if statement was TRUE. Note that "Hey!" is never printed. It'll work the same way for your function.

作为一个加号,你可以使用返回值让其他函数(如main())知道函数终止,因为用户赢了(返回1)或丢失(返回0)。

As a plus, you can use that return value to let OTHER functions (such as main()) know if the function terminated because the user won (it returned 1), or lost (it returned 0).

是的,break语句也是终止循环的有效方法,但我认为这种方法是更安全,更清洁的方法。一般来说,我们想知道函数或程序是否成功。

Yes, a break statement is also a valid way to terminate the loop, but I submit that this method is the safer, cleaner way to go about it. In general, we like to know whether a function or program was successful or not.

这篇关于一旦用户错误两次结束循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆