Ending a loop once the user is wrong twice?

ぐ巨炮叔叔 提交于 2019-12-14 04:01:36

问题


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;

every time he's wrong, but didnt work.

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

回答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;
}

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.

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).

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.




回答2:


You can use a break; statement if the person has gotten the answer wrong twice.

As per comments. You can shed the do while loop in favour of one for loop. Just put a break at the bottom if the wrong guesses are 2




回答3:


There are several great suggestions for refactoring the code to remove the duplication of effort here, but to get the program functioning immediately, you've got to break out of the for loop surrounding the do { } while(wrong < 2) loop.

A simple way to do this is to modify the for loop to test the wrong variable also. The added benefit is, if I'm reading everything correctly, you'll no longer need the do{ } while(); loop.

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



来源:https://stackoverflow.com/questions/9221627/ending-a-loop-once-the-user-is-wrong-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!