Thread: Hangman game

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    19

    Hangman game

    So I made a hangman game but it won't compile...I'm so lost

    Code:
    
    #include    <stdio.h>
    #include    <string.h>
    #include    <ctype.h>
    
    
    #define MAXWORD    20 //max word length
    #define INCORRECT_GUESSES 9 //max # of guesses allowed
    
    
    /* Prototypes */
    void    fill_array( char *theArray, int howMany, char theLetter ); // Fills theArray with howMany copies of theLetter
    int     get_letter( char *theWord, char *soFar ); // Get char from player, checks the letter, shows progress so far
    int     letter_in_word( char *theWord, char *soFar, char theLetter ); // Check if letter is in word, updates progress so far
    void    lower_string( char *someWord ); // Convert the word to lowercase
    void    play( char *theWord ); // Play one game
    
    
    
    
    /* Function definitions */
    
    
    int main( )
    {
        char theWord[ MAXWORD ]; //theWord is an array of characters with the length of MAXWORD 
        char *someWord; //the word that will be read from the file
    
    
        FILE *ifp = fopen( "guesswords.txt", "r" ); //open myFile to read
    
    
        if (ifp == NULL ) //if file doesn't exist
        {
            printf( "File does not exist\n" );
    
    
            return -1; //return code
        }
    
    
        
        fscanf(ifp, "%s", theWord); //scans word from file
        
        lower_string(someWord); //invokes the function lower_string
        play(theWord); //passing theWord into play function
    
    
        fclose(ifp); //closes file
    
    
        return 0; //all done
    }
    
    
    /* *********************************************************************** */
    
    
    void fill_array( char *theArray, int howMany, char theLetter ) // Fills theArray with howMany copies of theLetter
    {
        int i;
        for( i = 0; i < howMany; ++i)
        {
            theArray[i] = theLetter;
        }
    }
    
    
    /* *********************************************************************** */
    
    
    // Get char from player, checks the letter, shows progress so far
    int get_letter( char *theWord, char *soFar )
    {
        char theLetter;
    
    
        printf("Please enter a letter: \n");
        scanf("%c", &theLetter);
    
    
        theLetter = tolower(theLetter);
    
    
        letter_in_word(theWord, soFar, theLetter);
         
    }
    
    
    /* *********************************************************************** */
    
    
    int letter_in_word( char *theWord, char *soFar, char theLetter ) // Check if letter is in word, updates progress so far
    {
        int i;
        int numberOfMatches;
    
    
        for( i = 0; i < strlen(theWord); ++i )
        {
            if (theWord[i] == theLetter)
            {
                soFar[i] = theLetter;
                ++numberOfMatches;
            }
    
    
            if (numberOfMatches == 0)
            {
                printf("Oops! The letter you guessed is not in the word!\n");
                return 0; //not found
            }
    
    
            else 
            {
                printf("Yay! The letter you guessed is in the word!\n");
                printf("%s", soFar);
                return 1; //1 or more was found
            }
        }
    
    
        return numberOfMatches;
    }
    
    
    /* *********************************************************************** */
    
    
    void lower_string ( char *someWord ) // Convert the word to lowercase
    {
        int i;
        for (i = 0; i < strlen(someWord); ++i)
        {
            someWord[i] = tolower(someWord[i]);
        }
    }
    
    
    /* *********************************************************************** */
    
    
    void play( char *theWord ) // Play one game
    {
        char theWord[MAXWORD]; 
        int maxGuesses = INCORRECT_GUESSES;
        char soFar[MAXWORD];
        int result;
        int i;
    
    
        fill_array( soFar, strlen(theWord), '*' );
    
    
        printf("%s", soFar);
    
    
        for (int i = 0; i < INCORRECT_GUESSES; ++i)
        {
            result=get_letter(theWord, soFar);
    
    
            if ( result = 0 )
            {
                printf("You have %d more letters to guess! Keep guessing!", i);
            }
    
    
            else if ( result = 1 )
            {
                printf("You have guessed all letters! Good job!");
            }
    
    
            printf("The word was %c", theWord);
    
    
        }
    }










    // End of program

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The first thing to do is compile will maximum warnings.

    Code:
    main.c||In function 'play':|
    main.c|145|error: 'theWord' redeclared as different kind of symbol|
    main.c|143|note: previous definition of 'theWord' was here|
    main.c|158|error: redeclaration of 'i' with no linkage|
    main.c|149|note: previous declaration of 'i' was here|
    main.c|158|error: 'for' loop initial declarations are only allowed in C99 mode|
    main.c|158|note: use option -std=c99 or -std=gnu99 to compile your code|
    main.c|163|warning: suggest parentheses around assignment used as truth value|
    main.c|169|warning: suggest parentheses around assignment used as truth value|
    main.c|175|warning: format '%c' expects type 'int', but argument 2 has type 'char *'|
    main.c|146|warning: unused variable 'maxGuesses'|
    ||=== Build finished: 3 errors, 4 warnings ===|
    There's no reason to get near 200 lines of code with some of these warnings/errors. (Also note that some of these are self-explanatory.)

    Read this (A development process) to see how to build up a program piece by piece, making sure it works each step of the way. Then give it another shot.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Next time, please copy-paste the complete compiler errors/warnings along with line numbers, it's makes our job helping you much easier.

    Well, for starters, I would guess your first problem is writing 179 lines of code without ever compiling and testing it. You should work in small chunks. Write 5-10 lines of code at a time, compile it with maximum warning level, and fix all errors and warnings. Then do some simple tests to make sure it works as intended. Then write another 5-10 lines, go through the same process, etc, until you're done.

    Here's what I get when I compile:
    Code:
    cagarvin@cagarvin-goobuntu:~/sandbox/cprogramming$ make foo
    gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘play’:
    foo.c:145:10: error: ‘theWord’ redeclared as different kind of symbol
    foo.c:143:18: note: previous definition of ‘theWord’ was here
    foo.c:163:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    foo.c:169:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    foo.c:175:9: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
    foo.c:149:9: warning: unused variable ‘i’ [-Wunused-variable]
    foo.c:146:9: warning: unused variable ‘maxGuesses’ [-Wunused-variable]
    foo.c: In function ‘get_letter’:
    foo.c:86:1: warning: control reaches end of non-void function [-Wreturn-type]
    make: *** [foo] Error 1
    To fix those problems

    • In play(), you have a parameter named theWord, and a local variable named theWord. You can't do that, they're two different entities, so they would need two different names. Though I suspect you can do without the local variable entirely, and just use the parameter version.
    • One = is for assignment, two == is for comparison.
    • %c is for printing a single char. If you want to print a string, you need %s. Refer to the printf documentation for more info.
    • Remove the unused variables
    • get_letter is supposed to return an int, so return one, or change the function to return void.


    Other problems:

    • scanf with %c will read the guesses the user types in, but also the newline everytime they press enter. See this FAQ article for ideas: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com.
    • Avoid using strlen in your loop conditions. That calls strlen every time, which is inefficient. Use a temporary variable instead (see below), or check for the null terminator as your stopping condition
    • Are you using someWord or theWord in main? If you want to use someWord, you can't just have a char *, you must actually have room to store the string. I think you can remove someWord and only use theWord.


    There's maybe more problems, but that should cover it for now. Oh yeah, the example:
    Code:
    for (int i = 0; theWord[i] != '\0'; i++)
    // or
    int len = strlen(theWord);
    for (int i = 0; i < len; i++)

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    19
    Thank you so much!! I have a better idea of what's going wrong now.
    One thing - the for loop in play has thing error message associated with it: error C2143: syntax error : missing ';' before '{'

    EDIT:
    Just kidding, I realize what's wrong - i is both declared & initialized in the same loop.
    Thanks for the help!
    Last edited by GaitBait; 12-06-2013 at 01:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman Game
    By Mcdom34 in forum C# Programming
    Replies: 3
    Last Post: 10-13-2012, 11:21 AM
  2. Hangman game
    By Dontgiveup in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2011, 04:44 PM
  3. Help with hangman game.
    By astarialexi in forum C Programming
    Replies: 8
    Last Post: 03-13-2011, 04:04 AM
  4. Hangman Game - Need Help!!
    By krobort in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2006, 04:15 PM
  5. New game: Hangman!
    By abrege in forum Game Programming
    Replies: 6
    Last Post: 12-05-2002, 02:05 PM