Thread: Letter Guessing Game

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    4

    Letter Guessing Game

    I have no errors, but the game isn't working correctly. When I input a letter it states that it is too high even if the letter is "z". It will also only let me input 2 guesses instead of the 5. I am new to C so I really need help.

    Code:
    #define _CRT_SECURE_NO_WARNINGS 1
    #include <stdio.h>
    #define MAXGUESSES 5
    
    
    //function prototypes with a comment for each one describing what the function does.
    //Copy and paste from assignment
    
    
    //this function provides instructions to the user on how to play the game
    void GameRules( );
    
    
    //this function runs one entire game. It for checks either 5 incorrect guesses or a correct guess.
    //It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
    int SingleGame(char file_letter);
    
    
    //this function prompts the player to make a guess and returns that guess
    //this function is called from inside the SingleGame( ) function described above
    char  RetrieveGuess(  );
    
    
    //this function takes two arguments, the guess from the player 
    //and the solution letter from the file. 
    //It lets the user know if the guess comes alphabetically before or after the answer 
    //The function returns 1 if the guess matches the solution and returns a 0 if they do not match
    //this function is called from inside the OneGame( ) function described above
    int GuessedIt(char answer, char input_letter);
    
    
    
    
    int main()
    {
    	//declare additional variables
    	int PlayGames = 4,
    	i = 0;
    	char letter;
    	char input_letter;
    	char answer;
    	FILE* fptr;
    	//display instructions
    	//open file
    	fptr = fopen("lettersin.txt", "r");
    	
    	GameRules( );
    	
    	//get number of games to play
    	printf("How many games would you like to play today: ");
    	scanf(" %d", &PlayGames);
    
    
    	input_letter = RetrieveGuess(  );
    	for(i=0;i<PlayGames;i++)
    	{
    		//get a letter from file
    		fscanf(fptr, " %c", &letter);
    		printf("This is game %d\n", i);
            answer = SingleGame(letter);
    		printf("The letter is: %c\n", letter);
    		//Play one game
    		//check for win or lose	
    		
    	}
    	
    	//close file
    	fclose(fptr);
    	return 0;
    }
    
    
    //Function definitions
    //copy and paste prototypes and comments from assignment. Then remove the ; from each
    
    
    //this function provides instructions to the user on how to play the game
    void GameRules( )
    {
    	printf("Welcome to the Letter Guessing Game!\n");
    	printf("You will have a chance to play between 1 to 4 games\n");
    	printf("You will have 5 chances to guess each letter\n");
    	printf("Remember that they are all in lowercase\n");
    	printf("Let's Begin:\n");
    	return;
    }
    
    
    //this function prompts the player to make a guess and returns that guess
    //this function is called from inside the SingleGame( ) function described above
    char  RetrieveGuess(  )
    {
    	char input_letter = 0;
    	printf("Enter a guess: ");
    	scanf(" %c", &input_letter);
    	return input_letter;
    }
    //this function takes two arguments, the guess from the player 
    //and the solution letter from the file. 
    //It lets the user know if the guess comes alphabetically before or after the answer 
    //The function returns 1 if the guess matches the solution and returns a 0 if they do not match
    //this function is called from inside the OneGame( ) function described above
    int GuessedIt(char answer, char input_letter)
    {
    	if(input_letter==answer)
    	{
    		printf("You guessed it!!!\n");
    		return 1;
    	}
    	else if(input_letter>answer)
    	{
    		printf("Your answer %c is too high\n", input_letter);
    		RetrieveGuess(  );
    		return 0;
    	}
    	else if(input_letter<answer)
    	{
    		printf("Your answer %c is too low\n", input_letter);
    		RetrieveGuess(  );
    		return 0;
    	}
    	
    }
    
    
    int SingleGame(char file_letter)
    {	
    	int numGuesses = 0;
    	while(numGuesses < MAXGUESSES)
    	{	
    		//RetrieveGuess function call
    		char RetrieveGuess(  );
    		//GuessedIt function call
    		int GuessedIt(char answer, char input_letter);
    		//update counter for number of guesses used
    		numGuesses = numGuesses +1;
    		if(numGuesses>MAXGUESSES)
    		{
    			printf("You have run out of guesses\n");
    		}
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    hi

    The first thing I came across was a Return missing in Function "GuessIt", all the returns shown are within the If-else, if it does not go into the "if" there is no matching return for the Function. You also call another function that returns data, but you do nothing with the return data ?

    I know my compiler does not like a return for a void return type function GameRules Function..

    Also the "SingleGame" Function, you seem to be passing arguments to a function that don't exist in the calling functiion.. not sure why you are re-declaring them in the function..?

    John
    Last edited by JohnGM; 09-24-2015 at 05:28 PM.

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    4
    I took the returns out of the if else and put it outside the statement. Is this what you meant?

    Code:
    int GuessedIt(char answer, char input_letter)
    {
        if(input_letter==answer)
        {
            printf("You guessed it!!!\n");
        }
        else if(input_letter > answer)
        {
            printf("Your answer %c is too high\n", input_letter);
            RetrieveGuess(  );
        }
        else if(input_letter<answer)
        {
            printf("Your answer %c is too low\n", input_letter);
            RetrieveGuess(  );
        }
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2015
    Posts
    4
    I have figured it out. I have RetrieveGuess() in places it's not suppose to be in and some of the variables are swapped. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with c letter guessing game
    By Daniel Aarons in forum C Programming
    Replies: 2
    Last Post: 02-10-2015, 04:33 PM
  2. Letter Guessing Game
    By jlowe2013 in forum C Programming
    Replies: 11
    Last Post: 09-24-2014, 06:32 PM
  3. Replies: 8
    Last Post: 09-26-2012, 05:20 AM
  4. Debug Assertion Fail in letter guessing game
    By kevinjaems in forum C Programming
    Replies: 4
    Last Post: 06-16-2012, 09:27 PM
  5. Need help with letter guessing program
    By ltdec in forum C Programming
    Replies: 31
    Last Post: 10-05-2011, 12:29 AM