hangman in functions assignment from michael dawson's book

This is a discussion on hangman in functions assignment from michael dawson's book within the C++ Programming forums, part of the General Programming Boards category; In Michael Dawson's book, " Beginning C++ Game Programming ", there was and exercise saying to rewrite a hangman game ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    Post hangman in functions assignment from michael dawson's book

    In Michael Dawson's book, " Beginning C++ Game Programming ", there was and exercise saying to rewrite a hangman game learned earlier in the book using functions. I tried all i could to get it run correctly, but i got nowhere. Can someone tell me what I am doing wrong?

    I'm a little new to programming, so what i did probably looks dumb. Haha.

    well here it is....

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <ctime>
    #include <cctype>
    
    using namespace std;
    
    const int MAX_WRONG = 8;
    int WRONG;
    
    string Guess(string soFar, string used);
    string check_guess(string guess, string used, string soFar, string THE_WORD);
    
    int main()
    {
        vector<string> words;
        words.push_back("AMAZING");
        words.push_back("DIFFICULT");
        words.push_back("COMPATABLE");
    
        srand(time(0));
        random_shuffle(words.begin(), words.end());
        const string THE_WORD = words[0];
        WRONG = 0;
        string soFAR(THE_WORD.size(), '-');
        string used = "";
    
        cout << "Welcome to Hangman. Good luck!\n";
    
        while ((WRONG < MAX_WRONG) && (soFAR != THE_WORD))
        {
        string guess(string soFar, string used);
        string check_guess(string guess, string used);
        }
    
        if (WRONG == MAX_WRONG)
        {
            cout << "\nYou've been hanged!";
        }
        else
        {
            cout << "\nYou guessed it!";
        }
        cout << "\nThe word was " << THE_WORD << endl;
    
        return 0;
    }
    
    string Guess(string soFar, string used)
    {
        cout << "\n\nYou have " << (MAX_WRONG - WRONG) << " incorrect guesses left.";
        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);
        while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed ";
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper (guess);
        }
    
        used += guess;
    
        return guess, used;
    }
    
    string check_guess(string guess, string used, string soFar, string THE_WORD)
    {
        if (THE_WORD.find(guess) != string::npos)
        {
            cout << "That's right! " << guess << " is in the word.";
    
            for (int i = 0; i < THE_WORD.length(); ++i)
            {
                cout << THE_WORD[i];
            }
        }
        else
        {
                cout << "Sorry, " << guess << " isn't in the word.";
                ++WRONG;
        }
    
        return guess;
    }
    All help appreciated, thank you.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,334
    O_o

    Examine your code for your use of your "custom" functions.

    Soma

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    I remember playing hangman as a kid, and the word was never revealed because I guessed one letter right. You need to think harder on that.

    Study the difference calling a function and declaring a function: you're getting it wrong.

    Also, don't try to return more than one thing from functions. It wont work like you expect.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    well this isn't like homework or anything I'm learning this through the book and online tutorials, so can u please tell me specifically what i have done wrong. Thank you for the help.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    Then why are you reading a game programming book when you don't understand the basics of the language? Start with a newbie book.
    You are declaring functions instead of calling them, and you are trying to return multiple values from the same function.
    It's apparent that you lack the knowledge of how the basics work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    Scratch out the other code i wrote...

    Okay, thank you guys for what you said it helped me a lot. I rewrote the code the best i can, but the program does not work still.

    Haha, i just decided to go back and see what i can do while in the middle of writing this. I found what i did was not the best i can do.

    I fixed a couple of things and the program works exactly the same as the code written without functions.

    Thank you for the help.

    Here's the code...

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <ctime>
    #include <cctype>
    
    using namespace std;
    
    string used = "";
    int wrong = 0;
    char guessed;
    
    void guess()
    {
        cout << "\n\nEnter your guess: ";
        cin >> guessed;
        guessed = toupper(guessed);
        while (used.find(guessed) != string::npos)
        {
            cout << "\nYou've already guess ";
            cout << "Enter you guess: ";
            cin >> guessed;
            guessed = toupper(guessed);
        }
    
        used += guessed;
    }
    
    string checkguess(string soFAR, const string WORD)
    {
        if (WORD.find(guessed) != string::npos)
        {
            cout << "That's right! " << guessed << " is in the word.\n";
    
            for (int i = 0; i < WORD.length(); ++i)
            {
                if (WORD[i] == guessed)
                {
                    soFAR[i] = guessed;
                }
            }
        }
        else
        {
            cout << "Sorry, " << guessed << " isn't in the word.\n";
            ++wrong;
        }
        return soFAR;
    }
    
    int main()
    {
        const int MAX_WRONG = 8;
    
        vector<string> words;
        words.push_back("AMAZING");
        words.push_back("FICTICIOUS");
        words.push_back("DIFFICULT");
    
        srand(time(0));
        random_shuffle(words.begin(), words.end());
        const string THE_WORD = words[0];
        string soFar(THE_WORD.size(), '-');
    
        cout << "Welcom to Hangman. Good luck!\n";
    
        while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
        {
            int x;
            cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.";
            if (x == 0)
            {
                cout << "\nSo far, the word is:\n" << soFar << endl;
            }
            cout << "\nYou've used the following letters:\n" << used << endl;
    
    
        guess();
        soFar = checkguess(soFar, THE_WORD);
        cout << "\nSo far, the word is:\n" << soFar << endl;
        if (x < 1)
        {
        ++x;
        }
        }
    
        if (wrong == MAX_WRONG)
        {
            cout <<"\nYou've been hanged!";
        }
        else
        {
            cout << "\nYou guessed it!";
        }
        cout << "\nThe word was " << THE_WORD << endl;
    
        return 0;
    }
    -Joel

    ps. I'm reading the book cause i felt i missed some things (this would be one of them)...
    Last edited by joellllmal; 07-21-2010 at 11:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 03:18 PM
  3. hangman game, copied staight from book.. 31 errors?!?!?!?!
    By Blizzarddog in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2002, 12:20 AM
  4. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 07:03 PM

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