Sign in to follow this  

Hangman Crashes!

This topic is 3459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I'm sure some of you have read the book Beginning C++ Game Programming by Michael Dawson. I'm currently going through the book, but I'm having trouble with one of the exercises in Chapter 5. In chapter 4, it gives you the source code for the Hangman game, and the exercise is to develop the Hangman game using functions. Well...I did, and it seems good to me, but it's not working :( The program crashes every time I go and run it. It compiles just fine though..can anyone spot what's going wrong in my program? Thanks for any help.
//Hangman

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

const int MAX_WRONG = 8;
    
vector<string> words;
    
const string THE_WORD = words[0];
int wrong = 0;
string soFar(THE_WORD.size(), '-');
string used = "";

char getGuess(string);
void checkGuess(char);

int main()
{
    srand(time(0));
    random_shuffle(words.begin(), words.end());
    words.push_back("GUESS");
    words.push_back("HANGMAN");
    words.push_back("DIFFICULT");
    
    cout << "Welcome to Hangman. Good luck!" << endl;
    
    while((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
                 cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
                 cout << "\nYou've used the following letters:\n" << used << endl;
                 cout << "\nSo far, the word is:\n" << soFar << endl;
                 
                 char guess = getGuess("\n\nEnter your guess: ");
                 checkGuess(guess);
    }
    
    if (wrong == MAX_WRONG)
       cout << "\nYou've been hanged!";
    else
        cout << "\nYou've guessed it!";
    
    cout << "\nThe word was " << THE_WORD << endl;
    
    return 0;
}

char getGuess(string prompt)
{
     char guess;
     cout << prompt;
     cin >> guess;
     guess = toupper(guess);
     while (used.find(guess) != string::npos)
     {
           cout << "\nYou've already guessed " << guess << endl;
           cout << "Enter your guess: ";
           cin >> guess;
           guess = toupper(guess);
     }
     
     used += guess;
     return guess;
}

void checkGuess(char guess)
{
    if(THE_WORD.find(guess) != string::npos)
    {
                            cout << "That's right! " << guess << " is in the word.\n";
                            
                            for (int i = 0; i < THE_WORD.length(); ++i)
                                if (THE_WORD[i] == guess)
                                   soFar[i] = guess;
    }
    else
    {
        cout << "Sorry, " << guess << " isn't in the word.\n";
        ++wrong;
    }
}

Share this post


Link to post
Share on other sites
It looks like you're trying to access elements from words before any elements have been added to it.

[Edit: I'd recommend using at() rather than operator[]() where you can get away with it, and/or making sure that range-checking is turned on. Also, be sure to read through the code carefully rather than simply copy-and-pasting it. For example, what exactly do you hope to accomplish by shuffling an empty array, and then adding elements to it afterwards?)

Share this post


Link to post
Share on other sites

This topic is 3459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this