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

Thread: Hangman

Threaded View

  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

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