Thread: Stuck on Letter Guessing Game program

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Stuck on Letter Guessing Game program

    Hey everyone! I am new to C and was given the Letter Guessing Game assignment. I have been stuck on some errors I don't know really how to resolve in time, and I've been reading the forums and similar issues with the same assignment all different code, and saw that it helped me understand, I need help resolving the errors and understanding what went wrong so I can learn from it.LtrGame.c
    Hey everyone! I am new to C and was given the Letter Guessing Game assignment. I have been stuck on this
    Attached Images Attached Images Stuck on Letter Guessing Game program-error-png 

  2. #2
    Registered User
    Join Date
    Sep 2016
    Posts
    2
    Here is the code if you don't feel like downloading the c file

    insert
    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.
    
    
    //Provides user with the instructions on how to play the game
    void GameRules(void);
    
    
    //It runs one entire game. It checks either five incorrect guesses or one correct guess.
    //This also returns a 0 if the game ended with the player not guessing the correct letter, if not it returns a 1.
    int SingleGame(char file_letter);
    
    
    //Prompts a player to make a guess and returns said guess
    //It is located inside the single game function from above
    char RetrieveGuess();
    
    
    //Takes two arguments, both the guess from the player and the solution from the file
    //Lets the player know if the guess comes alphabetically before or after the answer
    //It returns a 1 if the guess matches, and returns a 0 if it does not match
    //This is located inside the OneGame() funciton from above
    int GuessedIt(char answer, char input_letter);
    
    
    //Copy and paste from assignment
    
    
    int main()
    {
    	
    	
    	//declare additional variables
    	
    	int PlayGames = 4,
    		i = 0;
    	
    	char file_letter,
    		answer,
    		input_letter;
    	
    	int GuessedIt(char answer, char input_letter);
    	//display instructions
    	GameRules();
    		//open file
    		FILE * inPtr;
    
    
    	inPtr = fopen("lettersin.txt", "r");
    
    
    	//get number of games to play
    	printf("How many games do you wish to play?\n");
    	scanf(" %d", &PlayGames);
    	for (i = 0; i<PlayGames; i++)
    	{
    		
    		//get a letter from file
    		fscanf(inPtr, " %c", &file_letter);
    		
    		//Play one game
    		SingleGame(file_letter);
    		printf("The letter is %c\n", file_letter);
    		//check for win or lose
    		GuessedIt(answer, input_letter);
    	}
    	//close file
    	fclose(inPtr);
    	return 0;
    }
    //Function definitions
    //copy and paste prototypes and comments from assignment. Then remove the ; from each
    
    
    //Provides user with the instructions on how to play the game
    void GameRules(void)
    {
    	printf("Letter Guessing Game\n");
    	printf("Input how many times you want to play the game\n");
    	printf("You have 5 chances to guess each of the letters\n");
    	printf("Start\n");
    }
    
    
    
    
    
    
    int SingleGame(char file_letter)
    {
    	int numGuesses = 0;
    	while (numGuesses < MAXGUESSES)
    	{
    		
    		//RetrieveGuess function call
    		RetrieveGuess();
    		//GuessedIt function call
    		GuessedIt(answer, input_letter);
    		//update counter for number of guesses used
    		printf("number of guesses is %d\n", numGuesses);
    		numGuesses = numGuesses + 1;
    	
    	}
    	
    //Prompts a player to make a guess and returns said guess
    //It is located inside the single game function from above
    	char RetrieveGuess()
    {	
    	char input_letter;
    	printf("Enter a letter: ");
    	scanf(" %c", &input_letter);
    	return input_letter;
    
    
    }
    
    
    
    
    //Takes two arguments, both the guess from the player and the solution from the file
    //Lets the player know if the guess comes alphabetically before or after the answer
    //It returns a 1 if the guess matches, and returns a 0 if it does not match
    //This is located inside the OneGame() funciton from above
    int GuessedIt(char answer, char input_letter)
    {
    	char value;
    	if (answer < input_letter)value = 0;
    	else value = 1;
    	printf("letter comes before");
    	return 0;
    }
    
    
    
    
    }

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >GuessedIt(answer, input_letter);

    How does the SingleGame function know what answer is? You have declared it within
    function main not as an argument to the function.
    Double Helix STL

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by Ben0729 View Post
    Hey everyone! I am new to C and was given the Letter Guessing Game assignment. I have been stuck on some errors I don't know really how to resolve in time, and I've been reading the forums and similar issues with the same assignment all different code, and saw that it helped me understand, I need help resolving the errors and understanding what went wrong so I can learn from it.LtrGame.c
    Hey everyone! I am new to C and was given the Letter Guessing Game assignment. I have been stuck on this
    Assignments like these have the weakness that the best way of writing that particular program doesn't really scale up. Also, few real program conduct a dialogue with the user via stdin, if you do write such a program for real, then you need to spend a lot of time validating input, which for this program would be 90% of the code.

    The letters are read from a file. Is it known at compile time? Is it known that it contains only letters? In reality, you can read the entire file into memory as a first step and user will get bored after a tiny number of games, but is that allowed or should the input be unbounded?
    Then he gets five guesses at a letter. The test is so trivial you don't really need a function for it. However make the game a bit more complicated, and it becomes sensible to set up a "GAME" structure containing game state (the letter, maybe the guesses, the number of guesses he has left). Then you apply the rules, without doing any IO. IO if done by a separate driving function.

    So the logic goes

    Code:
    int main(void)
    {
       GAME *game;
    
       do
       {
           game = setupgame( ); /* possibly pass parameters */
           printf("Rules are this and that");
          
           /* game tells us when it has reached completion */
           while(!gameover(game))
           {
               do
              {
                  char input[1024];
                  /* the game tells us what prompt to give the user */
                  char *str = game_prompt(game);
                  printf("%s", str);
                  fgets(input, 1024, stdin);
                  /* we test that user has entered somethign acceptable,
                      and re-prompt until the game accepts the input*/
               } while(!game_inputacceptable(game, input));
               
               /* now we actually enter the input and oput the game
                   inot a new sate */
               game_enter_input(game, input);l
              
           }
           printf("Game over, result was %s\", game_finalresult|(game));
    
            destroy_game(game);
       } while(userstillwantstoplay());
    }
    The point is that the input /output logic is pretty much separated out from the details of the game. They are two different problems.

    Now we can replace "guess the letter" with a wide variety of possible games.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Letter Guessing Game
    By Kiken_ in forum C Programming
    Replies: 3
    Last Post: 09-24-2015, 05:31 PM
  2. help with c letter guessing game
    By Daniel Aarons in forum C Programming
    Replies: 2
    Last Post: 02-10-2015, 04:33 PM
  3. Letter Guessing Game
    By jlowe2013 in forum C Programming
    Replies: 11
    Last Post: 09-24-2014, 06:32 PM
  4. Replies: 8
    Last Post: 09-26-2012, 05:20 AM
  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