Search code examples
c++charprojectuser-inputloop-counter

Problems with reading char input and with a loop counter


Ok so I am having a major issue here, and after spending two days on this, I can not find out why it is not working as I think it should.

First problem: I have a function to see if the player wished to play again by inputting a y or n. If they press n, it terminates as it should, but if they press Y or y it just asks them again if they want to play again, until any other char other than y is pushed that is.

My second issue, and the most annoying, is the loop counter I am using to determine if the char they entered as a guess was used or not in the word (hangman game). It worked before, after I made some minor changes, it no longer works as intended, and the butt of it is that I didn't mess with that code at all, it was elsewhere. Essentially, the loop counter is reset to 0 every time the loop encounters the user guess within the word, but if the counter equals the length of the word, that means that the user guess was not found, and it should set the display value to plus one. However, all it does NOW is stay at zero all the time, even if the user enters an incorrect guess, which should increment the display value by one, it remains at zero. I have spent two days trying to get JUST this part to work, and it WAS working yesterday, but not today, and I can use another set of eyes on it to see if I overlooked something!

void playHangman(string wordArray[], bool usedWords[])
{
    string secretWord;
    unsigned seed = time(0);
    srand(seed);
    int wordChoice = (rand()%20);
    int counter = 0;
    int display = 0;
    bool winner = false;
    int wordLength = secretWord.length();
    int count;
    char again;

    do
    {
        while(usedWords[wordChoice])
        {
            if(wordChoice == 19)
                wordChoice = 0;
            else if(counter == 20)
            {
                for(int i = 0; i < SIZE; i++)
                    usedWords[i] = false;
                wordChoice = (rand()%20);
                counter = 0;
            }
            wordChoice++;
            counter++;
        }
        secretWord = wordArray[wordChoice];
        const char *word = new char [secretWord.length()];
        word = secretWord.c_str();
        char *userPrompt = new char [secretWord.length()];
        for(int i = 0; i < secretWord.length(); i++)
            userPrompt[i] = '_';
        userPrompt[secretWord.length()] = '\0';
        char userGuess = '\n';

        while(!winner)
        {
            count = 0;
            for(int i = 0; i < secretWord.length(); i++)
                cout << userPrompt[i] << " ";
            cout << "\n" << endl;
            displayGallows(display);

            if(display == 6)
            {
                cout << "Sorry, you lost!" << endl;
                break;
            }

            cout << "Enter a letter: ";
            cin >> userGuess;
            cin.ignore();
            for(int i = 0; i < secretWord.length(); i++)
            {
                if(word[i] == userGuess)
                {
                    userPrompt[i] = userGuess;
                    count = 0;
                }
                else if(count == (wordLength - 1))
                    display++;
                count++;
            }
            winner = checkWin(word, userPrompt, display, secretWord);       
        }
        again = playAgain();
    }while(again == 'Y' || again =='y');
}

char playAgain()
{
    char playAgain;
    cout << "Would you like to play again? Enter y or n: ";
    cin >> playAgain;
    return playAgain;

}

Solution

  • There are really two questions:

    1. Why doesn't it restart a game? Answer: because the program thinks that the game was successfully played. You set up your variables in front of the loop and you don't reset them to play another game. Recommendation: create a function which actually plays the game and just call it from the outer loop. Play the game in that function.
    2. Why doesn't it increment count? Dunno. Why you think count always stays at 0? However, it seems the condition count == (wordLength - 1) is unlikely to every become true because wordLength is set to the size of secretWord when secretWord happens to be empty (i.e. wordLength is set to 0) and never changed afterwards.