Thread: help with c letter guessing game

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    1

    help with c letter guessing game

    Trying to get this game to work. it builds right but the code is off. Can someone tell me what I am doing wrong?
    Code:
    #define _CRT_SECURE_NO_WARNINGS 1
    #include <stdio.h>
    #include <ctype.h>
    
    
    #define MAXGUESSES 5
    
    
    //Taken from paper printed from blackboard assignment
    
    
    //This function provides instructions to user.
    void Instructions( );
    
    
    //This function runs one game.
    
    
    //It returns a 0 if the game is over.
    int PlayGuess(char solution);
    
    
    //This function prompts the player to make a guess.
    
    
    //This function is called from inside the PlayGuess( ) function.
    char  GetLetter(  );
    
    
    //This function takes two arguments, the guess from the player and the solution letter from the file. 
    //The function returns 1 if the guess matches the solution and returns a 0 if they do not match
    int CompareLetters(char guess, char solution);
    
    
    int main()
    {   
    int i=0;
    int numgames=0;
    char solution;
    char guess;
    int compareletter(char guess, char solution);
    FILE *inp;
    inp = fopen("inputLet.txt","r");
    fscanf(inp,"%c",&solution);
    Instructions();
    //# of game player wants to do
    printf("Please enter the number of games you want to play\n");
    scanf("%d",& numgames);
    for(i=1;i<=numgames;i++)
    //display the value of game
    { 
        //get letter to guess from file
        fscanf(inp, "%c", &solution);
            PlayGuess(solution);
            printf("\nThe letter is %c\n" , solution);
    }
    
    
    
    
    fclose(inp);
    
    
    }
    void Instructions( )
    {
    printf("Letter guessing game\n");
    printf("Enter how many time you want to try(1 – 4 games)\n");
    printf("You have 5 chances to guess each letter\n");
    printf("Start now\n");
    }
    
    
        
    int PlayGuess(char solution) //player defined guesses.
    { 
        int numGuesses=0;
        int winOrLose =0;
        while(numGuesses < MAXGUESSES)
        {
            GetLetter();
    
    
            numGuesses = numGuesses +1;
            if(numGuesses>MAXGUESSES)
        {
            printf("You have run out of guesses\n");
        }
        }
        
        return 0;
    }
    
    
    //gets guess form player(getLetter function).
    char GetLetter()
    {
        char guess=0;
        char solution;
        printf("Enter a guess:", guess);
        scanf(" %c",&guess);
        CompareLetters(guess,solution);
        return guess;
    }
    
    
    //compare the guess and the solution
        //returns 1 if same
    // message based on before or after alphabetically
        //returns 0 if guess & answer arn't same
    
    
    int CompareLetters(char guess, char solution)
    { 
    if (guess==solution) //if answer is right
    {    printf("Great Job!\n");
    return 1;
    }
    else
    if (guess<solution)
    {
    printf("letter you are looking for comes after %c\n", guess);
    printf("\ntry harder\n");
    GetLetter();
    
    
    return 0;
    }
    else
        if (guess>solution)
    { printf("letter you are lookingfor comes before %c", guess);
    printf("\ntry harder\n");
    GetLetter();
    return 0;
    }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For starters, make sure your code is neatly formatted and indented. For example:

    Code:
    #define _CRT_SECURE_NO_WARNINGS 1
    #include <stdio.h>
    #include <ctype.h>
    
    #define MAXGUESSES 5
    
    //Taken from paper printed from blackboard assignment
    
    //This function provides instructions to user.
    void Instructions( );
    
    //This function runs one game.
    //It returns a 0 if the game is over.
    int PlayGuess(char solution);
    
    //This function prompts the player to make a guess.
    //This function is called from inside the PlayGuess( ) function.
    char  GetLetter(  );
    
    //This function takes two arguments, the guess from the player and the solution letter from the file.
    //The function returns 1 if the guess matches the solution and returns a 0 if they do not match
    int CompareLetters(char guess, char solution);
    
    
    int main()
    {
        int i=0;
        int numgames=0;
        char solution;
        char guess;
        int compareletter(char guess, char solution);
        FILE *inp;
    
        inp = fopen("inputLet.txt","r");
        fscanf(inp,"%c",&solution);
        Instructions();
    
        //# of game player wants to do
        printf("Please enter the number of games you want to play\n");
        scanf("%d",& numgames);
        //display the value of game
        for(i=1;i<=numgames;i++)
        {
            //get letter to guess from file
            fscanf(inp, "%c", &solution);
            PlayGuess(solution);
            printf("\nThe letter is %c\n" , solution);
        }
    
        fclose(inp);
    }
    
    void Instructions( )
    {
        printf("Letter guessing game\n");
        printf("Enter how many time you want to try(1 – 4 games)\n");
        printf("You have 5 chances to guess each letter\n");
        printf("Start now\n");
    }
    
    int PlayGuess(char solution) //player defined guesses.
    {
        int numGuesses=0;
        int winOrLose =0;
        while(numGuesses < MAXGUESSES)
        {
            GetLetter();
    
            numGuesses = numGuesses +1;
            if(numGuesses>MAXGUESSES)
            {
                printf("You have run out of guesses\n");
            }
        }
    
        return 0;
    }
    
    //gets guess form player(getLetter function).
    char GetLetter()
    {
        char guess=0;
        char solution;
    
        printf("Enter a guess:", guess);
        scanf(" %c",&guess);
        CompareLetters(guess,solution);
    
        return guess;
    }
    
    
    //compare the guess and the solution
        //returns 1 if same
    // message based on before or after alphabetically
        //returns 0 if guess & answer arn't same
    int CompareLetters(char guess, char solution)
    {
        if (guess==solution) //if answer is right
        {
            printf("Great Job!\n");
            return 1;
        }
        else if (guess<solution)
        {
            printf("letter you are looking for comes after %c\n", guess);
            printf("\ntry harder\n");
            GetLetter();
            return 0;
        }
        else if (guess>solution)
        {
            printf("letter you are lookingfor comes before %c", guess);
            printf("\ntry harder\n");
            GetLetter();
            return 0;
        }
    }
    Second, make sure your compiler warnings are turned up. Here are some I received when I compiled:

    Code:
    /*
    main.c||In function 'main':|
    main.c|30|warning: unused variable 'guess'|
    main.c||In function 'PlayGuess':|
    main.c|64|warning: unused variable 'winOrLose'|
    main.c|61|warning: unused parameter 'solution'|
    main.c||In function 'CompareLetters':|
    main.c|118|warning: control reaches end of non-void function|
    main.c||In function 'GetLetter':|
    main.c|87|warning: 'solution' is used uninitialized in this function|
    ||=== Build finished: 0 errors, 5 warnings ===|
    */
    Those last two warnings are pretty significant.

    Lastly, you need to be more descriptive with your problem. "The code is off" is not a sufficient description. Tell us what is not working; what input you give it, what output you get, what output you expect. Since your program relies on reading a file, you should post the contents of that file, as well.

    Without a decent problem description, it will not be easy to help you. I did notice one thing while reading through it quickly. The variable "solution" in "GetLetter()" is not the same variable as "solution" in "PlayGuess()". The correct way to do this would be to pass the variable "solution" to "GetLetter()". Even better would be to just have "GetLetter()" read a character and return it back to "PlayGuess()", and do the comparison there. Keeping your logic organized will help with development and troubleshooting.

    Also, you should check that the file has opened successfully before trying to access it

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Daniel Aarons View Post
    Trying to get this game to work. it builds right but the code is off. Can someone tell me what I am doing wrong?
    You might want to increase the warning level of your compiler (-Wall option for gcc, else consult your compiler docs). Warnings, while not strictly illegal C code (e.g. not a grammatical error like forgetting a ; or whatever), are generally signs of code that might cause trouble.
    Code:
    $ make foo
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:40:10: warning: unused variable ‘guess’ [-Wunused-variable]
         char guess;
              ^
    foo.c: In function ‘PlayGuess’:
    foo.c:78:9: warning: unused variable ‘winOrLose’ [-Wunused-variable]
         int winOrLose =0;
             ^
    foo.c: In function ‘GetLetter’:
    foo.c:100:5: warning: too many arguments for format [-Wformat-extra-args]
         printf("Enter a guess:", guess);
         ^
    foo.c: In function ‘CompareLetters’:
    foo.c:136:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    foo.c: In function ‘GetLetter’:
    foo.c:102:19: warning: ‘solution’ is used uninitialized in this function [-Wuninitialized]
         CompareLetters(guess,solution);
                       ^
    You're doing 3 things that result in undefined behavior (see Question 11.33 for more info on what exactly undefined behavior means)
    1. Calling printf with the wrong number of args.
    2. Not returning a proper value from a function with non-void return type.
    3. Using uninitialized variables.

    You need to fix those first.

    Then, if it still doesn't work, post back with a decent description of the problem. What input do you give that causes trouble? What (incorrect) output do you get? What should the correct output be?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Letter Guessing Game
    By jlowe2013 in forum C Programming
    Replies: 11
    Last Post: 09-24-2014, 06:32 PM
  2. Replies: 8
    Last Post: 09-26-2012, 05:20 AM
  3. Debug Assertion Fail in letter guessing game
    By kevinjaems in forum C Programming
    Replies: 4
    Last Post: 06-16-2012, 09:27 PM
  4. Need help with letter guessing program
    By ltdec in forum C Programming
    Replies: 31
    Last Post: 10-05-2011, 12:29 AM
  5. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 10:53 AM

Tags for this Thread