Thread: Letter guessing game segfault?

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    3

    Letter guessing game segfault?

    Hello, I am very new to programming and am having an issue with this letter guessing game. I believe the error is a segmentation fault but haven't been able to find the root problem.

    I know that the program executes perfectly the first time the function GuessTheLetter is called but then when my return statement at the end of that function runs my program seems to return to the function GuessTheLetter at which point it will execute again but with an unknown value for letter.

    I don't understand why it is returning to the function call instead of the start of the for loop

    (the program is formatted better in the attachement)


    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <ctype.h>
    #define MAXGUESSES 5
    
    //this function provides instructions to the user on how to play the game
    void LetterGuessRules();
    
    //this function runs one game.
    //input: character from the file, void return type
    //all other functions to Play one round of a game
    //are called from within the GuessTheLetter function
    void GuessTheLetter(char solution);
    
    //this function prompts the player to make a guess and returns that guess
    //this function is called from inside the GuessTheLetter( ) function described above
    char GetTheGuess();
    
    //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
    //This function also lets the user know if the guess comes alphabetically before or after the answer
    int CompareLetters(char let1, char let2);
    
    
    int main()
    {
                    //all declarations for function main
        FILE * LtrLst;
        int numGames, i = 0;
        char letter;//letter from file
    
                    //display game rules
        LetterGuessRules();
                    //Ask and get number of games to play
        printf("So, how many times will you be wagering your soul? ");
        scanf("%d", &numGames);
                    //connect to the file HINT: use fopen
        LtrLst = fopen("letterList.txt", "r");
                    //this for loop will allow the player to play more than one game
                    //without recompiling
        for (i = 0; i < numGames; i++)
        {
            //get a solution letter from file - use fscanf
            fscanf(LtrLst, "%c", &letter);
            //change the solution to lowercase
            letter = tolower(letter);
            //print the solution back onto the screen to test
            printf("%c", letter);
            //call the GuessTheLetter function and pass it the solution
            GuessTheLetter(letter);
    
    
        }
        
        //close file pointer
        fclose(LtrLst);
        return 0;
    }
    
    //this function provides instructions to the user on how to play the game
    void LetterGuessRules()
    {
        //display game rules
        printf("So you wanna play my little game? The rules are simple, what (letter) do I have in my pocket? You have 5 guesses to figure out what letter I have. Fail and you will be subject to eternity locked in cyber space!\n");
        return 0;
    }
    
    //this function runs one game. 
    //input: character from the file, void return type
    //all other functions to Play one round of a game 
    //are called from within the GuessTheLetter function
    //this function lets the user know if they have won or lost the game
    void GuessTheLetter(char solution)
    {
        int win = 0;
        int numGuesses = 0;
        char userguess;
        int trialrem;
        //declare additional variables 
    
        while (numGuesses < MAXGUESSES && win == 0)
        {
            trialrem = 5 - numGuesses;
            printf("You have %d guess left ", trialrem);
            //get a guess from the user  by calling the GetTheGuess function
            userguess = GetTheGuess();    
            //change the guess to lowercase
            userguess = tolower(userguess);
    
            //win = call the function to compare the guess with the solution
            win = CompareLetters(solution, userguess);
            numGuesses++;//count the number of guesses so far
        }
        //use conditions to let the user know if they won or lost the round of the game
        if (win == 1)
            printf("You chose... Wisely ");
        else
            printf("You chose... poorly ");
    
        return 0;
    }
    
    //this function prompts the player to make a guess and returns that guess
    //this function is called from inside the GuessTheLetter( ) function described above
    char GetTheGuess()
    {
        char input;
        printf("What is your guess?\n");
        scanf(" %c", &input);
        return input;
    }
    
    //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
    //This function also lets the user know if the guess comes alphabetically before or after the answer
    int CompareLetters(char let1, char let2)
    {
        if (let1 == let2)
        {
            return 1;
        }
        else if (let1 < let2)
        {
            printf("\nMy letter comes before your guess\n");
            return 0;
        }
        else
        {
            printf("\nMy letter comes after your guess\n");
            return 0;
        }
    }
    Attached Files Attached Files
    Last edited by LetoMultus; 10-10-2016 at 09:07 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well the first obvious thing to check is that the file was opened successfully.

    Code:
        LtrLst = fopen("letterList.txt", "r");
        if ( LtrLst != NULL ) {
            // all your existing code goes here
        } else {
            printf("Failed to open the file\n");
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    3
    Quote Originally Posted by Salem View Post
    Well the first obvious thing to check is that the file was opened successfully.
    Tested as you suggested. it is opening and reading the file

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    In main you may need a space before the %c in the fscanf format.

    You should remove the "return 0" statements from the void functions (void functions don't return anything).

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    3
    Quote Originally Posted by algorism View Post
    In main you may need a space before the %c in the fscanf format.

    You should remove the "return 0" statements from the void functions (void functions don't return anything).
    I had found those errors just before seeing this actually but it still wont run properly. Thank you for looking so closely at the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread