CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Hangman

  1. #1
    Join Date
    Apr 2010
    Posts
    9

    Hangman

    I am having trouble with a hangman program. Every time I try to compile, it tells me that variables haven't been declared in functions. Below is the code; I need help with the "evaluate" function specifically. That's where the variables are showing as undeclared. How do you use values from main() in other functions?
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <ctime>
    #include <cctype>
    
    using namespace std;
    const int MAX_WRONG = 8;
    char getText(void);
    void evaluate(char, string, string, const string, int);
    int main()
    {
        vector<string> words;
        words.push_back("GUESS");
        words.push_back("HANGMAN");
        words.push_back("DIFFICULT");
    
        srand(time(0));
        random_shuffle(words.begin(), words.end());
        const string THE_WORD = words[0];
        int wrong = 0;
        string soFar(THE_WORD.size(), '-');
        string used = "";
        cout << "Welcome to Hangman. Good luck!\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 = getText();
            evaluate(guess, soFar, used, THE_WORD, wrong);
        }
        if (wrong ==  MAX_WRONG)
            cout << "\nYou've been hanged!";
        else
            cout << "\nYou guessed it!";
            cout << "\nThe word was " << THE_WORD << endl;
    
    
        return 0;
    }
    
    char getText()
    {
        char text;
        cout << "Enter your guess: ";
        cin >> text;
        text = toupper(text);
        return text;
    }
    void evaluate(char, string, string, const string, int)
    {
        while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed " << guess << endl;
            guess = getText();
        }
        used  += guess;
    
        if (THE_WORD.find(guess) != string::npos)
        {
            cout << "That's right, " << guess << " is in the word.\n";
            for (int = 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;
        }
    }
    Also, I'm starting to think c++ is beyond my mental capacity. Any suggestions on easier languages?
    Last edited by ZainAsad; June 6th, 2010 at 02:45 PM. Reason: error

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Hangman

    Quote Originally Posted by ZainAsad View Post
    Code:
    void evaluate(char, string, string, const string, int)
    {
        //...
    }
    Since you haven't given the function arguments a name, there is no way you can use them in your function. Change your function definition to
    Code:
    void evaluate(char guess, string soFar, string used, const string THE_WORD, int wrong)
    {
        //...
    }
    Also, I'm starting to think c++ is beyond my mental capacity. Any suggestions on easier languages?
    Which book are you using to learn C++?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 2010
    Posts
    9

    Re: Hangman

    Thanks for the help, I can get it to compile now. Now, there's another problem. The program can detect whether the guess is incorrect or not, but it doesn't update: what letters you've used(it just stays blank), soFar to include the characters to fill in the blanks, and how many incorrect guesses you have left. It just keeps on saying I have 8 guesses left. Whether a get a million wrong or not. Also, you can never win. It just keeps on telling you "Yes, " << guess << " is in the word! ", but it never lets you complete the game.

    The book I'm using is C++ Through Game Programming: Second Edition.

  4. #4
    Join Date
    Dec 2007
    Posts
    69

    Re: Hangman

    You're modifying the variables "soFar" and "wrong" in the evaluate function, but since you have passed them "by value" to the function, you're just modifying a copy of the variables, and it doesn't affect the ones declared in main().

    The solution is to pass them "by reference":
    Code:
    void evaluate(char guess, string &soFar, string used, const string THE_WORD, int &wrong)
    You should also change the function signature in the top:
    Code:
    void evaluate(char, string &, string, const string, int &);

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Hangman

    Quote Originally Posted by ZainAsad View Post
    The book I'm using is C++ Through Game Programming: Second Edition.
    I'm not familiar with that book, so I may be too quick to judge. However, looking at the table of contents I can see why you are running into these problems: the book seems to explain the STL (in one chapter!) before it explains functions, references and classes. Again, I don't have the book so I may be off here, but that doesn't seem logical to me.

    If you'd like to use a second book as a reference, have a look here. Bjarne Stroustrup's book covers everything quite detailed. Accelerated C++ is a good beginners book if you want to do real programming from the outset. I'm not familiar with other beginners books.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Apr 2010
    Posts
    9

    Re: Hangman

    @GameZelda: Thank you for the assistance. I could also set new variables, and then make those variables the return values of the functions. In turn, those return values could be the modified values "soFar" and "wrong", couldn't they? At least, that's what I understand from the book.
    @D_Drmmr: Are you referring to the book Principles and Practice Using C++? I think it would be beneficial to have a book written by the actual creator of C++! As he would know the innards of it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured