Hangman Error

This is a discussion on Hangman Error within the Game Programming forums, part of the General Programming Boards category; Hey guys, I searched before I decided to post but to no avail. Basically, my situation is that I'm learning ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    Hangman Error

    Hey guys,

    I searched before I decided to post but to no avail. Basically, my situation is that I'm learning C++ through a really informative book called Beginning C++ Game Programming by Michael Dawson. I just reached a section where the author demonstrates how to create the source code for a game of Hangman. I copied his version of the game word for word and tried to compile it (I got a used version of the book, so the CD that contains all the original files discussed in the book is not in my possession), but it gave me quite a few compile errors--which may be due to syntax errors, but I can't seem to locate any.

    This is what the program looks like:
    Code:
    //Hangman
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <ctime>
    #include <cctype> // cctype is part of the standard lib and it includes functions for converting
    //characters to uppercase, which is used in order to compare individual letters
    
    int main()
    {
        //setup
        const int MAX_WRONG = 8; //max number of incorrect guesses
        
        vector<string> words; //collection of possible words to guess
        words.push_back("CONGLOMERATION");
        words.push_back("TELEOLOGY");
        words.push_back("TACITURN");
        
        srand(time(0));
        random_shuffle(words.begin(), words.end()); //words are shuffled so index position '0' will be random
        const string THE_WORD = words[0];
        int wrong = 0;
        string soFar(THE_WORD.size(), '-');
        string used = "";
        
        cout << "Welcome to Hangman!\n";
        
        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;
              
              cout << "\n\nEnter your guess: ";
              cin >> guess;
              guess = toupper(guess); //make uppercase since secret word is in uppercase
              while (used.find(guess) != string::npos)
              {
                    cout << "\nYou've already guess " << guess << endl;
                    cout << "Enter your guess: ";
                    cin >> guess;
                    guess = toupper(guess);
              }
              
              used += guess;
              
              if (THE_WORD.find(guess) != string::npos)
              {
                                       cout << "That's right! " << guess << " is in the word.\n";
                                       
                                       //update soFar to include newly guessed letter
                                       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;
              }
        }
        //shut down
        if (wrong == MAX_WRONG)
           cout << "\nYou've been hanged!";
        else
            cout << "\nYou guessed it!";
        cout << "\nThe word was " << THE_WORD << endl;
        
        system("Pause");
        
    }
    And here's the error it's giving me: "`vector' undeclared (first use this function) ". I'm using Dev-C++ version 4.9.9.2. I can't find what the error is, I'm sure it is something really simple that I'm just not seeing. Any help would be very much appreciated from this noob programmer!

    Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    This will at least make it compile.
    Code:
    //Hangman
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <ctime>
    #include <cctype> // cctype is part of the standard lib and it includes functions for converting
    //characters to uppercase, which is used in order to compare individual letters
    using namespace std;    //!! added
    
    int main()
    {
        //setup
        const int MAX_WRONG = 8; //max number of incorrect guesses
    
        vector<string> words; //collection of possible words to guess
        words.push_back("CONGLOMERATION");
        words.push_back("TELEOLOGY");
        words.push_back("TACITURN");
    
        srand(time(0));
        random_shuffle(words.begin(), words.end()); //words are shuffled so index position '0' will be random
        const string THE_WORD = words[0];
        int wrong = 0;
        string soFar(THE_WORD.size(), '-');
        string used = "";
    
        cout << "Welcome to Hangman!\n";
    
        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;    //!! removed mid-line ;
              cout << "\nSo far, the word is:\n" << soFar << endl;
    
              char guess;
    
              cout << "\n\nEnter your guess: ";
              cin >> guess;
              guess = toupper(guess); //make uppercase since secret word is in uppercase
              while (used.find(guess) != string::npos)
              {
                    cout << "\nYou've already guess " << guess << endl;
                    cout << "Enter your guess: ";
                    cin >> guess;
                    guess = toupper(guess);
              }
    
              used += guess;
    
              if (THE_WORD.find(guess) != string::npos)
              {
                                       cout << "That's right! " << guess << " is in the word.\n";
    
                                       //update soFar to include newly guessed letter
                                       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;
              }
        }
        //shut down
        if (wrong == MAX_WRONG)
           cout << "\nYou've been hanged!";
        else
            cout << "\nYou guessed it!";
        cout << "\nThe word was " << THE_WORD << endl;
    
        system("Pause");
    
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    Wow, I'm embarrassed! I can't believe I forgot the namespace line. And yeah, I didn't catch that line where I put a semicolon right in the middle.

    Oh well, thanks a million, I really appreciate you helping me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hangman
    By alshidi in forum C++ Programming
    Replies: 6
    Last Post: 01-01-2011, 03:57 AM
  2. hangman
    By jstrike21 in forum C Programming
    Replies: 8
    Last Post: 10-24-2008, 04:03 PM
  3. hangman.c help
    By barneygumble742 in forum C Programming
    Replies: 3
    Last Post: 11-11-2004, 07:55 PM
  4. Help doing Hangman...
    By Kreative in forum C Programming
    Replies: 11
    Last Post: 08-18-2002, 09:22 PM
  5. Hangman again
    By Kat in forum C Programming
    Replies: 11
    Last Post: 07-19-2002, 12:49 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21