# C++ hangman...Weird problem



## unibrow1990 (Dec 11, 2009)

Okay so I have an assignment for a class to write a hangman program in C++. The program is finished and works fine but I have a weird problem I have not been able to solve. 

The problem is that between two of my function calls I keep getting a blank line that waits for input printed, and I cannot figure out where it's coming from. 

Here is the function that is having the problem, I bolded the two lines between which this strange prompt appears.


```
void PlayGame(bool loadWord)

/*
 * Purpose: Function that actually plays through a game of hangman.
 * Given: A bool that tells the program to either load a phrase from the file or let the user input one manually.
 * Return: Nothing.
 */

{
    string phrase; // The phrase being solved.
    int wrongCount = 0,
            guessCount = 0; // Kepps track of the number of wrong guesses and the number of total guesses.
    string guess; // The users guess.
    char guesschar; // The first character of the users guess.
    bool continuePlay = true; // Flag checking whether gameplay should continue.
    string guessList; // The list of letters the user has guessed.
    bool win = true; // Flag for if the user has won.

    if(loadWord) // If the user askes for a word from the file, that is where a word is retireved from.

    {
        phrase = GetPhrase("HWords.txt");
    }

    else // Otherwise the user enters a phrase themselves.

    {
        phrase = GetPhrase();
    }

    bool winList[phrase.size()]; // Boolean array that monitors if the user has guessed everything yet.

    do

    {
        system("CLS"); // Clear the screen

        ShowImage(wrongCount); // show the current hangman image.
        cout << endl;
        PrintPhrase(guessList, phrase); // Print the phrase in its current state.
        cout << endl;
        [B]PrintGuessList(guessList); // Print the letters the user has guessed.

        guess = GetGuess(guessList, phrase); // Get a guess from the user.[/B]

        if(guess == phrase) // If the user guesses the entire phrase they win.

        {
            win = true;
            break;
        }

        if(guess == "quit") // If the user enters quit they lose(unless the phrase happens to be "quit").

        {
            win = false;
            break;
        }

        guesschar = guess[0]; // Otherwise the first char of the guess is taken.

        if(!CheckPhrase(guesschar, phrase)) // If the letter is not in the phrase the user loses a guess.

        {
            wrongCount++;
        }

        guessList = guessList + guesschar; // The current guess is added to the guesslist.
        guessCount++;

        FindPhraseStatus(guessList, phrase, winList); // Check if the phrase has been solved.

        continuePlay = !CheckForWin(winList, phrase.size()); // Checks if the user has won.

        if(wrongCount == 7) // If the user guesses wrong 7 times they lose.

        {
            continuePlay = false;
            win = false;
        }
    }
    while(continuePlay);

    system("CLS"); // Clear the screen
    ShowImage(wrongCount); // Show the final image.
    cout << phrase; // Show the solved phrase.

    EndGame(win); // Go to the endgame menu.
}
```

and here are the two functions being called in those lines:



```
void PrintGuessList(string guessList)

/*
 * Purpose: To print all of the letters the user has already guessed.
 * Given: A string containing all of the guessed letters.
 * Return: Nothing.
 */

{
    cout << "\nYou have guessed: ";

    for(int i = 0; i < guessList.size(); i++)

    {
        cout << guessList[i] << ' '; // Prints each letter guessed followed by a space.
    }

    cout << endl;
}

/*******************************************************************************
 
 *******************************************************************************/

string GetGuess(string guessList, string phrase)

/*
 * Purpose: To get a guess from the user.
 * Given: A list of letters the user has already guessed.
 * Return: A string the user enters.
 */

{
    string guess; // The user's guess.
    bool guessFail; // A flag that singlnals an invalid guess.

    do

    {
        cin.ignore(1000, '\n'); // clears the input buffer.

        guessFail = false;

        cout << "Guess a letter: ";
        getline(cin, guess); // Gets a guess from the user(can be a single letter, "quit, or the entire phrase.

        for(int i = 0; i < guess.size(); i++)

        {
            guess[i] = tolower(guess[i]); // Makes sure the guess is lowercase.
        }

        if(guess != "quit" || guess != phrase)

        {
            guessFail = CheckGuess(guessList, guess[0]); // Checks the first char of the guess to make sure it is appropriate.
        }

    }
    while(guessFail);

    return guess;
}
```

If you need any more information please let me know, and thanks in advance for any help.


----------



## Kreij (Dec 11, 2009)

I don't see anything that sticks out. (Could just be me. lol)
Can you put up a zip of the project source?
Much easier to help if I can use the debugger.


----------



## unibrow1990 (Dec 11, 2009)

Here is the entire NetBeans project, I was going through the code with my CS professor today and she had no idea what was wrong with it either.


----------



## Kreij (Dec 12, 2009)

Arrgh ... I deleted NetBeans from my computer a few days ago because I was not using it.

Still don't see any reason that you should getting a blank line input prompt.
Can you post a screen of the console when it happens?


----------



## unibrow1990 (Dec 12, 2009)

No problem.

here are two screenshots, the first with the blank line and then the next after I push enter and it gives the prompt.

Also I don't know how relevent it is but if I load a word from a file instead of entering it myself it doesn't give me the blank line the first time it prompts me for a guess, but it does every subsequent time.


----------



## Kreij (Dec 12, 2009)

Change the line that clears the input buffer to only do it if there is actually something in the buffer.

Can't test for syntax, but somthing like...
if (cin.length() != 0) cin.ignore(1000, '\n');

or maybe try ...  cin.ignore(1000, '\0');


----------



## unibrow1990 (Dec 12, 2009)

cin does not have a length function, and looking through all of the member functions netbeans is showing me I don't see anything that sounds like it would do the equivilent. I tried width() and rdbuf() but they didn't help.

When i try the second suggestion it prints out an infinite number of blank lines and never goes to the prompt.

EDIT: tried .gcount() too now and still no luck.


----------



## Kreij (Dec 12, 2009)

Sorry ... downloading NetBeans again. My C++ is a bit rusty, but I'll keep trying to help.


----------



## unibrow1990 (Dec 12, 2009)

Thank you very much, it's not a huge deal as the program still works but it's frustrating when you have a problem you can find no reason for.


----------



## Kreij (Dec 12, 2009)

It's not frustrating, it is duel between you and the dang computer. 
Rule #1 : The computer never wins.

Now !@#$ NetBeans doesn't want to seem to install. 
Now I have my own duel !!  

Okay, I forgot that I deleted the JDK (which is required for NB), would have been nice if the install said something. lol
Downloading JDK + NB now. Takes awhile on my rather slow satellite connection. Still better than dial-up though.
Actually I got better ping times with dial-up. Oh well ... I'll quit rambling.


----------



## unibrow1990 (Dec 12, 2009)

I feel you, I'm actually setting up NetBerans on my linux as we speak, The IDE installed fine, but I have to go through and install each plugin one at a time or it times out before finishing and doesn't install any of them


----------



## Disparia (Dec 12, 2009)

I CAST YE DEMONS OF ERROR OUT!!!! Not so good with C++, so that's all I can do...

Kriej, sounds like we need to set up a carrier pigeon/jump drive network with you


----------



## Kreij (Dec 13, 2009)

When I left off, the JDK and NetBeans installed fine.
The C++ pugligin installed fine, but when I try to open his project it says I have no c++ compiler.


sigh... Off to figure out this little issue.


----------



## Kreij (Dec 13, 2009)

Loaded minGW! Yay
It's doesn't include it's own make file. Boo
Deleted minGW.
Loaded Cygwin! Yay.
Default download didn't included the C++ compilers. Boo
Downloaded the Cygwin Development stuff! Yay
It's can't find conio. Boo

Stay tuned for more in the saga of trying to NetBeans to compile Uni's project ....


----------



## unibrow1990 (Dec 13, 2009)

Thats a problem I can help with! 
You need to get msys with mingw for the makefiles, and I at least had to install gdb as a seperate file with mingw, it didn't come with it.
I can't help at al with cygwin.


----------



## Kreij (Dec 13, 2009)

There seems to be some contention in the ranks that using conio (in particular getch) in combination with iostream is not a good thing. 
The reason I didn't D/L msys was that it loaded a whole bunch of crap I didn't want.
Turns out, so did the cygwin dev install. DOH !!

What a mess. Tell your teacher your switching to MS C++ Express. So much cleaner, and the IDE is much better (at least in my opinion).


----------



## unibrow1990 (Dec 13, 2009)

I have a copy of VS 2008(I get it free cause I'm a student) but I switched to NetBeans because I didn't like it. I now only use VS when I'm working with someone else's projects that are already set up for that.

As for the _getch thing I'll try commenting them out.


----------



## Kreij (Dec 13, 2009)

You didn't like VS2008?  Why?

I didn't get my problem worked out yet, I got sidetracked looking up how to cook Prime Rib.


----------



## unibrow1990 (Dec 13, 2009)

I don't really know exactly what it was about VS, I think it just feels to fat and bloated for me. I started out learning scripting languages like python, ruby, and common lisp. For all of those languages I just used notepad++ and an interpreter. 

I don't know what it is about netbeans, but to me it manages to feel like it has all of the features people go to VS for without feeling so...ugly 

I guess it's just a matter of to each his own.

Prime rib FTW 

EDIT: Features not fetuses.


----------



## CounterZeus (Dec 13, 2009)

can't compile hangman in VS2008...


----------



## Kreij (Dec 13, 2009)

unibrow1990 said:


> I don't really know exactly what it was about VS, I think it just feels to fat and bloated for me. I started out learning scripting languages like python, ruby, and common lisp. For all of those languages I just used notepad++ and an interpreter.



Yeah, when I started programming 25+ years ago there was only text editors and compilers. Everything was command line run.



> I don't know what it is about netbeans, but to me it manages to feel like it has all of the fetuses people go to VS for without feeling so...ugly



Fetuses? lol ... To me the more features an IDE has the better I like it. In my opinion, VS is a top of the line IDE that make programming and code maintenance a pleasure instead of a nightmare. But again, that is just my opinion.



> I guess it's just a matter of to each his own.



Indeed it is, and you should use the IDE that you are most comfortable with and which best suits you.



> Prime rib FTW



Yeah, now I'm hungry. lol



CounterZeus said:


> can't compile hangman in VS2008...


You will have to do a bit of code modification to make VS like it.


----------



## unibrow1990 (Dec 14, 2009)

CounterZeus said:


> can't compile hangman in VS2008...



Weird I just tried that too, it looks like VS doesn't like me using string.size() as an array size (I don't know why since it's a constant)

If you change the two boolean arrays that have string.size to use a literal number for size it will work fine.


----------



## Kreij (Dec 14, 2009)

I got NetwBeans and minGW and MSys installed here at work.
Now when I try to run the program is says it cant run program cmd.exe in the project directory (obviously as cmd does not live there).

Looks like I'm missing a setting. Any advice?

I'm getting close here.


----------



## unibrow1990 (Dec 14, 2009)

Did you add MinGW and MSys to your path variable in windows? Thats the only other thing I remember having to do to set up NetBeans.


----------



## Kreij (Dec 14, 2009)

No, I didn't see that in the instructions. I will check it out.


----------



## unibrow1990 (Dec 14, 2009)

All this work...I'm going to feel really bad if it turns out to be some stupid syntax/logic mistake on my part 


EDIT: Just finally got a chance to try commenting out the usages of _getch() and that did nothing.


----------



## unibrow1990 (Dec 16, 2009)

Turned in the project today, My CS prof. couldn't figure out the problem either so she's not going to mark me down for it. Thanks for all the help with this anyways Kreij.


----------



## Psychohyena (Jan 10, 2010)

Hey dude,

I was looking around on the internet to see what ideas were out there for getting random lines from a file (making a Hangman game for the kids) and I came across your post.

I've fiddled about with the code and the problem can be solved by changing where you call the cin.ignore.

```
string GetPhrase(string filename)
{
    cin.ignore(1000, '\n');
    ifstream fileIn;
```

In your function to get a user-entered phrase you call cin.ignore at the beginning but not in the from file version. This will clear it before you get to the actual game section.


```
string GetGuess(string guessList, string phrase)
{
    string guess; // The user's guess.
    bool guessFail; // A flag that singlnals an invalid guess.

    do

    {
         
        guessFail = false;

        cout << "Guess a letter: ";
        cin.ignore(1000, '\n');// clears the input buffer.
```
Here I have the code clearing the input buffer after making the request for a new letter but before one is entered. That should be both of your problems solved 

EDIT: Don't feel bad about it. Remember your teacher didn't see the problem either, anyways as a programmer it's your job to write the code and it's QA's job to make sure it works as expected.


----------



## Kreij (Jan 10, 2010)

Thanks for your input, Psychohyena, and welcome to TPU


----------



## unibrow1990 (Jan 10, 2010)

Thanks for giving it a look Psychohyena, but when I compiled it the solution you posted still doesn't work  .

Now I just get the blank line on the same line as "Guess a letter" and I have to put my input on the line below.

I really do appreciate you trying though, iI already tuned the project in so it's not a big deal if it can't be figured out(not that I still don't want to know  )

And welcome to TPU 


EDIT: as I read your post again it sounds like you actually got a working copy to compile with that code, what compiler were you using when you did that and what version of that compiler?


----------



## Psychohyena (Jan 13, 2010)

It was the MinGW Compiler version 3.4.5

I use Code::Blocks for all my programming.... makes things nice and easy to read hehe.


----------



## unibrow1990 (Jan 13, 2010)

Thats weird that it worked for you-- I've been using MinGW 5.1.6 -- I wonder if maybe I shouldn't reinstall my compiler maybe its just a dumb problem like that.


----------



## Psychohyena (Jan 13, 2010)

I've got the working copy saved, so I've attached it to this post so that we can do a test on the same code  I'm just wondering if maybe I forgot something or missed a step when going through the changes.

As you said while the assessment has been handed in and passed there's always that little part that wonders "WTH is wrong with it?"


----------



## unibrow1990 (Jan 13, 2010)

I just compiled the copy you uploaded and I have the same results as before...It must be a problem with my compiler if the exact same code is working correctly for you.


----------

