Search code examples
c++functionwhile-loopheaderfunction-call

How to end hangman game using "int promptYN(string reply)" in c++


In my hangman game I am having trouble ending the game. Using my header file I have defined...

#define PLAY 1 #define STOP 0 #define ERROR -1

... and then I defined function

int promptYN(string reply)
{
    reply = strToUpper(reply);
    if (reply == "YES", "OK", "SURE", "Y")
        return PLAY;
    else if (reply == "NO", "QUIT", "STOP", "TERMINATE", "N", "Q")
        return STOP;
    else
        return ERROR;
}

... now that the function has been called in main no matter what the user types the hangman game always runs, even if I respond with "NO", "QUIT", "STOP", "TERMINATE", "N", "Q".

Here is my while loop that I am using to try and play the game, I just need help figuring out when the user responds with "Y", then the game plays, "N" then the game stops, or ERROR and the game resets to asking the question again. Thank you!

    cout << "Do you want to play hangman? (y or n): ";
    cin >> userReply;
    gameStatus = promptYN(userReply);

    while (gameStatus != STOP)
    {
        if (gameStatus == PLAY)
        {
            chances = 0;
            cout << "Let's Play\n\n";

            guessWord = getNextWord();                  // Function call (randword.h)
            guessWord = strToUpper(guessWord);          // Function call (MyFuncts.h)
            cout << "\nWord to Guess: " << guessWord << endl;
            cout << endl << h1;


            while (wrong != 6)                          // Function to find out which hangman board to print to user
            {
                cout << "\nEnter a letter to guess: ";
                cin >> gLetter;
                gLetter = toupper(gLetter);
                cout << "You entered: " << gLetter << endl << endl;

                cout << gLetter << " is NOT in the word to guess.";

                wrong++;

                if (wrong == 0)
                    cout << endl << h1 << endl;
                if (wrong == 1)
                    cout << endl << h2 << endl;
                if (wrong == 2)
                    cout << endl << h3 << endl;
                if (wrong == 3)
                    cout << endl << h4 << endl;
                if (wrong == 4)
                    cout << endl << h5 << endl;
                if (wrong == 5)
                    cout << endl << h6 << endl;
                if (wrong == 6)
                    cout << endl << h7 << endl;
            }
        }

        else if (gameStatus == STOP)
        {
            cout << "Goodbye\n";
        }

        else
        {
            cout << "Error - please enter (y or n)\n";
        }
        cout << "Do you want to play hangman? (y or n): ";
        cin >> userReply;
        gameStatus = promptYN(userReply);
    }

Solution

  • This line:

    if (reply == "YES", "OK", "SURE", "Y")

    does not compare reply to each of those strings. To do that, you have to compare the strings individually:

    int promptYN(string reply)
    {
        if (reply == "YES" || reply =="OK" || reply == "SURE" || reply == "Y")
            return PLAY;
        //...
    }
    

    This will work, however a better way of setting this up in C++ is to use something like a std::set<std::string> and see if the value exists:

    #include <set>
    //...
    
    int promptYN(string reply)
    {
        static std::set<std::string> OKResponse = {"YES", "OK", "SURE", "Y"};
        static std::set<std::string> StopResponse = {"NO", "QUIT", "STOP", 
                                                     "TERMINATE", "N", "Q"};
        if ( OKResponse.count(reply) == 1 )
          return PLAY;
        else
        if (StopResponse.count(reply) == 1)
           return STOP;
        return ERROR;
    }
    

    The std::set::count will return 1 if the value exists in the set, otherwise 0 is returned.

    To add another response to either the "ok" or "stop" just requires adding more strings to each of the set values.

    Live Example

    Note: Make sure you have a compliant C++11 compiler, both for the setting up of the std::set, and to ensure that static works in a thread-safe manner.