Thread: Need Help with Letter Guessing Game

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

    Need Help with Letter Guessing Game

    There's something wrong with my code. It builds no problem, and it starts up okay. But when it asks to enter a letter it gives me a Run-time check failure #3-T error. If I choose to ignore it, I could keep entering guesses, but the error keeps coming back whenever I do.


    Code:
    #define _CRT_SECURE_NO_WARNINGS 
    #include <stdio.h>
    #include <ctype.h>
    
    
    
    
    #define MAXGUESSES 5
    
    
    
    
    
    
    
    
    
    
    
    
    void LetterGuessRules();//This function provides instructions to user
    int PlayOneGame(char solution);//This function runs one game, it returns a 0 if the game is over
    char  GetGuess(); //Gets guess from player
    int CompareGuessAndSoultion(char guess, char solution);//Takes two arguments, the guess from the player and the solution from the file. Returns a 1 if the guess is correct or a 0 is it's not correct
    int GameCount();
    
    
    int main()
    {
        int i = 0;
        int numgames = 0;
        char solution;
        char guess;
        int compareletter(char guess, char solution);
        FILE *inPtr;
        inPtr = fopen("letters.txt", "r");
        fscanf(inPtr, "%c", &solution);
        solution = toupper(solution);
        LetterGuessRules();
        //# of games player wants to play
        int GameCount();
        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(inPtr, "%c", &solution);
            solution = toupper(solution);
            PlayOneGame(solution);
            printf("\nThe letter is %c\n", solution);
        }
    
    
    
    
    
    
    
    
        fclose(inPtr);
    
    
    
    
    }
        void LetterGuessRules()
    {
        printf("Welcome to the Letter guessing game\n");
        printf("\nEnter how many games you want to play(1-8 games)\n");
        printf("You have 5 chances to guess each letter\n");
        printf("Let's begin!\n");
    }
    
    
    
    
    
    
        int PlayOneGame(char solution) 
    {
        int numGuesses = 0;
        int winOrLose = 0;
        while (numGuesses < MAXGUESSES)
        {
            GetGuess();
    
    
    
    
            numGuesses = numGuesses + 1;
            if (numGuesses>MAXGUESSES)
            {
                printf("You have run out of guesses\n");
            }
        }
    
    
        return 0;
    }
    
    
    
    
    
    
        char GetGuess()
    {
        char guess = 0;
        char solution;
        printf("Enter a guess:", guess);
        scanf(" %c", &guess);
        CompareGuessAndSoultion(guess, solution);
        return guess;
    }
    
    
    
    
    //Compares the guess and the solution
    //returns 1 if the guess is correct
    //returns 0 if guess & answer aren't the same
    
    
    
    
        int CompareGuessAndSoultion(char guess, char solution)
    {
        if (guess == solution) //If the guess was correct
        {
            printf("Great Job!\n");
            return 1;
        }
        else
            if (guess<solution)
            {
                printf("The letter you are looking for comes after %c\n", guess);
                printf("\nTry harder\n");
                GetGuess();
    
    
    
    
                return 0;
            }
            else
                if (guess>solution)
                {
                    printf("The letter you are looking for comes before %c", guess);
                    printf("\nTry harder\n");
                    GetGuess();
                    return 0;
                }
    }
        int GameCount()
        {
            
            int i = 0;
            int numgames;
            printf("Please enter the number of games you want to play\n");
            scanf("%d", &numgames);
            for (i = 1; i <= numgames; i++);
            
        }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Enable your compiler's warnings. You're passing an uninitialized variable, "solution", to your "CompareGuessAndSoultion()" function.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Letter guessing game segfault?
    By LetoMultus in forum C Programming
    Replies: 4
    Last Post: 10-11-2016, 12:18 PM
  2. Stuck on Letter Guessing Game program
    By Ben0729 in forum C Programming
    Replies: 3
    Last Post: 10-01-2016, 02:25 AM
  3. Letter Guessing Game
    By Kiken_ in forum C Programming
    Replies: 3
    Last Post: 09-24-2015, 05:31 PM
  4. help with c letter guessing game
    By Daniel Aarons in forum C Programming
    Replies: 2
    Last Post: 02-10-2015, 04:33 PM
  5. Letter Guessing Game
    By jlowe2013 in forum C Programming
    Replies: 11
    Last Post: 09-24-2014, 06:32 PM

Tags for this Thread