Confused Beginner - hangman game

This is a discussion on Confused Beginner - hangman game within the C++ Programming forums, part of the General Programming Boards category; This is pretty much my first attempt at anything in C++. I started off writing code in HTML then PHP ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    Confused Beginner - hangman game

    This is pretty much my first attempt at anything in C++. I started off writing code in HTML then PHP now I've moved on to C++.

    Specifically in this, I am having a problem with the menu function. When you compile and run the code and hit x + enter or any other letter, it loops -- endlessly. I'm having trouble figuring it out, any help would be appreciated. Thanks.

    Code:
    // Hangman
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    void menu();
    
    string make_display_word(string s) {
        string t = "";
        for (int i = 0; i < s.size(); i++)
        t += '_';
        return t;
    }
    
    bool check_entries(string entry) {
        for(int i=0; i<entry.size(); i++) {
        if(isalpha(entry[i])) {
        } else { return false;
        }
        }
        return true;
    }
    
    void play(string word, string guesses, string correct) {
            char guess;
            cout << "Please guess a letter, the word is " << word.size() << " letters long\n\n";
            cin >> guess;
            if(!(isalpha(guess))) { cout << "You must enter only letters\n\n"; play(word, guesses, correct); } // check if guessed char is only a letter.
            if(int(guesses.find(guess))<0) {
            if(int(word.find(guess))<0) {
                guesses += guess;
                cout << "Wrong Guesses: " << guesses << "\n\n";
            } else {
                for(int j=0; j < word.size(); j++) {
                    if(word[j]==guess) { correct[j]=guess; }
                }
                if(word==correct) { cout << "\nCongratulations you have the right word!  " << correct << "\n\n\n\n\n\n\n\n\n\n";  exit(0); } else {
                cout << "\"" << guess << "\" is a correct guess!\n" << "What you have so far: " << correct << endl << endl;
            }
            }
            } else {
                cout << "You already guessed that letter!\n\n";
                play(word, guesses, correct);
            }
            play(word, guesses, correct);
    
    }
    
    
    
    void begin_play() {
            string word;
            string guesses = "";
            cout << "Please enter the word for the game  ";
            cin >> word;
            if(check_entries(word)==false) { cout << "You must enter only letters.\n"; begin_play(); }
            play(word, guesses, make_display_word(word));
    }
    
    void menu() {
        int menuchoice;  // Input for menu selection
        cout << "1. Play!\n2. Instructions\n3. Rules\n\n";
        cin >> menuchoice;
        switch(menuchoice) {
            case 1:
                begin_play();
                break;
            case 2:
                cout << "Instructions to follow soon.\n\n\n\n";
                menu();
                break;
            case 3:
                cout << "Rules to follow soon.\n\n\n\n";
                menu();
                break;
            default:
                cout << "Please select a menu choice.\n\n\n\n\n\n";
                menu();
        }
        }
    
    
    int main()
    {
        cout << "Welcome to Hangman!\n\n";
        menu(); // calls menu function
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Code:
    void menu() {
        int menuchoice;  // Input for menu selection
        cout << "1. Play!\n2. Instructions\n3. Rules\n\n";
        cin >> menuchoice;
        switch(menuchoice) {
    The character X is not an integer. Your switch case statement is switching based off an integer value. There was a thread similar to this one posted recently discussing how to get around this. One way is to make sure the input is a digit with isdigit().

    edit: here... Some help for a Noobie!

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    The problem is that if you enter any non-integer value, the stream will go into "error mode." In this mode, it will simply refuse to read any data until you clear the error and flush the stream.
    There are two usual ways around this: either flush the input buffer, or read a string and convert it to appropriate type.
    There should be lots of examples of both floating around the board.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    Ok, I searched around a found a solution by placing the cin.clear() and cin.ignore after the cin >> line. It needs both there, and in this order for it to work.

    Code:
        cin >> menuchoice;
        cin.clear();
        cin.ignore();
        switch(menuchoice) {
    Does this happen to everyone and they have to find solutions as well? Or is it from a lack of ability in my coding? I guess I am wondering if this is human error (ie: me) causing the error or it would happen to any programmer -- most just know what to do to avoid such things?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by goodfornothing
    Does this happen to everyone and they have to find solutions as well?
    Yes

    Quote Originally Posted by goodfornothing
    Or is it from a lack of ability in my coding?
    No, and in fact being able to come up with a solution shows that you have some coding ability

    That said, cin.ignore() just ignores one character from the buffer. To properly ignore what is remaining on the buffer, you need to call it with more arguments.

    Another way of handling this is to read into a std::string using std::getline(), then initialise a stringstream with that string, and only then extract the desired value from the stringstream.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 12-04-2006, 04:16 PM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. Hangman Game - Part 2
    By tigrfire in forum C Programming
    Replies: 1
    Last Post: 11-28-2005, 02:20 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  5. u got me wrong fellas
    By clover in forum Game Programming
    Replies: 4
    Last Post: 10-18-2003, 04:23 PM

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