Question

I am writing hangman in c++ that also accepts commands from the user (so that they can see clues and quit the game).

I am currently implementing the commands, but can't seem to get it to work. I am stuck on a help command that uses a pause() function to let the user read the text.

The pause function does not seem to be working, even though it has worked earlier in the program.

Code So Far

Here is the code: HelloWorld.cpp

void pause( string msg = "Press enter to continue..." ){
    cout << msg;
    cin.ignore();
}

// game loop.
do {
    correctLetters = 0;

    nl();
    showLives(lives);
    nl(5);
    showWordGuessed(wordGuessed);
    nl(1);
    cout << "Guess a letter (type /help to see commands): ";
    cin >> guess;

    if (guess.size() == 1){
        for (int i = 0;i < 5;i++){
            if (word[i] == guess) wordGuessed[i] =  word[i];
        }
    } else if (guess[0] == '/'){
        if (guess == "/ans"){

        } else if (guess == "/clue"){

        } else if (guess == "/help"){
            nl();
            cout << "Typing /ans will show you the answer and quit the game.\n";
            cout << "Typing /clue will show you one unknown letter.\n";
            cout << "Typing /guess will allow you to guess the entire word.\n";
            cout << "Typing /hangman will quit the game.\n";
            cout << "Typing /help will show you this help message.\n\n";
            pause("Press enter to continue playing...");
        } else if (guess == "/guess"){

        } else if (guess == "/hangman"){
            return 0;
        } else {

        }
    } else {
        nl();

    }

    for (int i = 0;i < 5;i++){
        if (wordGuessed[i] ==  word[i]) correctLetters++;
    }

    win = ((correctLetters == 5) ? win = true : win = false);

} while (!win);

cout << "you win";
Was it helpful?

Solution

Your pause function does not quite do what you think it does.

Lets imagine reading some data from the input stream, like this:

int i;
std::cin >> i;

The user types in a number, and hits return. operator>> will pull characters out of cin until it finds one that cannot be converted to an int, which it will leave there. In the case of normal user input, the character that is left on the stream is the newline (\n).

When you come to call your pause function, you try to do this:

std::cin.ignore()

which will ignore one single character on the input stream. Only there's a stray \n on the stream from the last time someone entered some data! So pause returns immediately.

You need to take steps to clean your input stream after use, perhaps by doing this each time after you've used std::cin >> whatever.

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

This will drop everything after the last valid input character to the final newline. Your pause function will likely work a little better after that.

Alternatively, instead of using >>, you could use getline, which is designed to deal with newline-terminated text strings.

std::string stuff;
std::getline(std::cin, stuff);

if (stuff.length() > 1)
    std::cout << "Easy, tiger.\n";

getline removes the trailing \n for you, which will also help your pause function work better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top