0

Right now I am trying to get my scorecard to work for hangman. This is a game I can play as many times as I want and I want it to show my lowest score when I press 2 at the game menu. For some reason my logic isn't working. Can anyone help? I have attached the 3 sections of code necessary for this to work.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>

using namespace std;

int maxAttempts = 10; //max attempts possible
void poorStiff(int);
int wordFill(char, string, string&);


int main()
{


    int intro;
    int bodyPart = 0;
    ifstream wordIn; //file stream read in
    wordIn.open("WordList.txt"); //list of words used in this game
    //if (!wordIn.good()) { std::cerr << "open failed.\n"; exit(1); }
    char letter; //letter guessed
    int numWrongGuesses = 0; //counts the number of wrong guesses
    string theWord;
    string words[13];
    int play = 0;
    bool run = true;
    int makeYourSelection = 0;
    int bestScore = 11;

    while (run == true)
    {
        int attemptsGame = 0;
        // put a game menu to loop that asks if you want to keep playing or exit and go to scorecard
        cout << "            WELCOME TO THE GAME OF HANGMAN\n\n\n\n";
        cout << "    Press 1 to play the game, press 2 to exit to the scorecard \n\n";
        cout << "   You have 10 attempts to guess the words before you get hung \n\n";
        cin >> play;


        while (play == 1)//loops while user has entered 1, 0 exits the game
        {
            for (int i = 0; i < 13; i++) //labeled 13 and not words because 13 is a constant
            {
                wordIn >> words[i];//replaces the file words and puts them in an array and counts them out of the file
                cout << words[i] << endl;
            }
            srand(time(NULL));//to get a random word
            int n = rand() % 12;
            theWord = words[n]; //pulls word from file
            wordIn.close();

            string mystery(theWord.length(), '*'); //replaces word letters with asterisks



            while (numWrongGuesses < maxAttempts) // while the amount of guesses is less than the max wrong guesses
            {
                cout << mystery << endl << endl;
                cout << "You now have the length of the word represented by the *'s. \n\n";
                cout << "Guess a letter \n\n";
                cin >> letter;


                if (wordFill(letter, theWord, mystery) == 0) //fuction call
                {
                    bodyPart++;
                    poorStiff(bodyPart);
                    cout << "You have entered a letter that isn't in the word, guess again. \n\n";
                    numWrongGuesses++;
                    attemptsGame++;
                }
                else
                {
                    for (int i = 0; i < mystery.length(); i++)
                    {
                        if (theWord[i] == letter)
                        {
                            mystery[i] = letter;
                        }
                    }
                    cout << "You have found one of the letters. Congratulations! \n\n";
                    cout << "You have: " << maxAttempts - numWrongGuesses;
                    cout << " guesses left \n\n" << endl;
                }

                if (theWord == mystery) // the word is the same as mystery
                {
                    cout << theWord << endl;
                    cout << "\n\n Awesome, you guessed it. \n\n";
                    break;
                    if (attemptsGame < bestScore)
                    {
                        bestScore = attemptsGame;

                    }
                }
            }
            if (numWrongGuesses == maxAttempts) //when you run out of guesses
            {
                cout << "Too bad, you ran out of guesses and have been hung at the gallows. \n\n";
                cout << "The word you were trying to guess was " << theWord << endl;
                poorStiff(bodyPart);
            }
            cin.ignore();
            cin.get();

            break;
        }
            while (play == 2)
            {
                cout << "Best Scores: \n\n";
                cout << bestScore << endl;
                system("pause");
                return 0;
                break;
            }
        }
        system("pause");
        return 0;
    }


    int wordFill(char guess, string theWordSecret, string&guessWord) //function for determing if you guess a letter contained
    {
        int i;
        int hits = 0; //letter hits within the word
        int many = theWordSecret.length();

        for (i = 0; i < many; i++)
        {
            if (guess == guessWord[i])
                return 0;
            if (guess == theWordSecret[i])
            {
                guessWord[i] == guess;
                hits++;
            }
        }
        return hits;
    }

    void poorStiff(int bodyPart)
    {

        if (bodyPart == 1)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 2)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|      |" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 3)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 4)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|" << endl;
            cout << "|    / " << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 5)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|" << endl;
            cout << "|    / |" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 6)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|\." << endl;
            cout << "|    / | \." << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 7)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|\." << endl;
            cout << "|    / | \." << endl;
            cout << "|     /" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 8)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|\." << endl;
            cout << "|    / | \." << endl;
            cout << "|     / \." << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 9){
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|\." << endl;
            cout << "|    / | \." << endl;
            cout << "|     / \." << endl;
            cout << "|    / " << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 10)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|\." << endl;
            cout << "|    / | \." << endl;
            cout << "|     / \." << endl;
            cout << "|    /   \." << endl;
            cout << "_______________" << endl;
        }
    }

2 Answers2

1

For a start enable your compiler warnings, you'll get many from a quick look at your code.

Change this

guessWord[i] == guess;

to this

guessWord[i] = guess;


At this point

if (wordFill(letter, theWord, mystery) == 0) //fuction call
{    
  ...
  cout << "You have entered a letter that isn't in the word, guess again. \n\n";
}
else
{
    ...

    if (theWord == mystery) // the word is the same as mystery
    {
       cout << theWord << endl;
       cout << "\n\n Awesome, you guessed it. \n\n";
       break;
       ...
    }
 }

Here, when you are entering the first if, then you will enter the second if too, which doesn't make sense. That happens because theWord and mystery are both empty strings!

Also notice that break should be after this part of code:

if (attemptsGame < bestScore)
{
   bestScore = attemptsGame;    
}

because as it is, this part of code will never be executed.


I suggest taking at this answer (not relevant with your logical error)

System(“pause”); - Why is it wrong?

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

Did you try debugging? If you step through your code you will immediately find the problem, which is here:

            if (theWord == mystery) // the word is the same as mystery
            {
                cout << theWord << endl;
                cout << "\n\n Awesome, you guessed it. \n\n";
                break;    // problem here
                if (attemptsGame < bestScore)
                {
                    bestScore = attemptsGame;

                }
                // break should be here
            }

You're breaking out of your loop before your checking logic. So move your break statement to after the check, because that code never gets executed.

Reticulated Spline
  • 1,892
  • 1
  • 18
  • 20
  • +1 for the break that I didn't find at first glance (I found it now when examining the code, but you had already found it). – gsamaras Nov 07 '14 at 03:28