Thread: Letter Guessing Game

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    6

    Letter Guessing Game

    Hey everyone! I am new to C and was given the Letter Guessing Game assignment. I have been stuck on this "expected error" and "parse issue" error for a while now and was looking for some help. Thanks!


    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #define MAXGUESSES 5
    
    
    //this function provides instructions to the user on how to play the game
    void Instructions( );
    
    
    //this function runs one 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 letter);
    
    
    //this function prompts the player to make a guess and returns that guess
    //this function is called from inside the Instructions( ) function described above
    char GetUserLetter( );
    
    
    //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
    int CheckLetter(char userLetter, char correctLetter);
    
    
    int main ()
    {
        //declare additional variables
        char letter = 0;
        int gamesToPlay = 0;
        int i = 0;
        int uWINuLOSE=0;
        char solution;
        
        //display instructions
        Instructions();
        
        //open file
        FILE *inp;
        inp=fopen("game.txt", "r");
        
        //get number of games to play
        scanf(" %d", &gamesToPlay);
        
        for(i=0;i<gamesToPlay;i++)
        {
        //get a letter from file
            fscanf(inp, " %c", &letter);
            GetUserLetter();
        //Play one game (Call PlayOneRound function)
            int = SingleGame(char letter);
        //check for win or lose
            if(uWINuLOSE == 1)
            {
                printf("Good Job!! You Win!");
            }
            else if(uWINuLOSE == 0)
            {
                printf("You did not guess the letter.\nThe letter you were looking for was %c.\n\n",solution);
            }
        }
        
        //close file
        fclose(inp);
        return 0;
    }
    
    
    //this function provides instructions to the user on how to play the game
    void Instructions( )
    {
        printf("Welcome to the Letter Guessing Game\n");
        printf("To begin you will enter the number of games you want to play(1 – 7 games)\n");
        printf("You have 5 chances to guess each letter\n");
        printf("Let's begin:\n\nHow many games would you like to play? (1 - 7):");
        return;
    }
    
    
    
    
    //this function prompts the player to make a guess and returns that guess
    //this function is called from inside the PlayGame( ) function described above
    
    
    char GetUserLetter( )
    {
        char guess;
        printf("Enter a letter: ");
        scanf("%c",&guess);
        return guess;
    
    
    }
    
    
    //this function takes two arguments, the guess from the player and the answer 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 answer and returns a 0 if they do not match
    
    
    int CheckLetter(char userLetter, char correctLetter)
    {
        char value;
        if (userLetter > correctLetter)value=0;
        else value=1;
        printf("letter come before");
        return 0;
    }
    
    
    
    
    //Function definitions
    //copy and paste prototypes and comments from assignment. Then remove the ; from each
    
    
    
    
    int SingleGame(char letter)
    {
        int numGuesses = 0;
        int winOrLose = 0;//SHOULD BE INITIALZED TO 0
        
        while(numGuesses < MAXGUESSES && winOrLose == 0)
        {
            //GetUserLetter function call
            GetUserLetter();
            
            //CheckLetter function call
            CheckLetter(char userLetter, char correctLetter);
            
            //update counter for number of guesses used
            numGuesses = numGuesses +1;
            
        }
    
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I don't see any errors. You should post the actual errors if you expect people to help you with them.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    This is the expected identifier error

    Code:
    //Play one game (Call PlayOneRound function)
            int = SingleGame(char letter);
    This is the expected expression error

    Code:
    //CheckLetter function call
            CheckLetter(char userLetter, char correctLetter);

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    In the future please post the complete error messages exactly as they appear in your development environment.

    I suggest you review your text book on how to use functions, perhaps this link might shed some light on the issues: Functions

    Jim

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    Sorry about that, I'll post it with the error from now on. I still can't tell what the issue is with my function. Am I calling it wrong?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jlowe2013 View Post
    Am I calling it wrong?
    Yes. You don't need the parameter types when calling the function. Only when declaring or defining it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    I removed the parameters from the above errors and I'm still getting a parse issue: expected expression error. Any ideas?

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    show the updated code.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #define MAXGUESSES 5
    
    
    //this function provides instructions to the user on how to play the game
    void Instructions( );
    
    
    //this function runs one 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 letter);
    
    
    //this function prompts the player to make a guess and returns that guess
    //this function is called from inside the Instructions( ) function described above
    char GetUserLetter( );
    
    
    //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
    int CheckLetter(char userLetter, char correctLetter);
    
    
    int main ()
    {
        //declare additional variables
        char letter = 0;
        int gamesToPlay = 0;
        int i = 0;
        int uWINuLOSE=0;
        char solution;
        
        //display instructions
        Instructions();
        
        //open file
        FILE *inp;
        inp=fopen("game.txt", "r");
    	
        //get number of games to play
        scanf(" %d", &gamesToPlay);
    	for(i=0;i<gamesToPlay;i++)
    	{
        
        //get a letter from file
            fscanf(inp, " %c", &letter);
            GetUserLetter();
        
        //Play one game (Call PlayOneRound function)
            SingleGame(i);
        
        //check for win or lose
            
            if(uWINuLOSE == 1)
            {
                printf("Good Job!! You Win!");
            }
            else if(uWINuLOSE == 0)
            {
                printf("You did not guess the letter.\nThe letter you were looking for was %c.\n\n",solution);
            }
        }
    	
    	//close file
    	fclose(inp);
        return 0;
    }
    
    
    //this function provides instructions to the user on how to play the game
    void Instructions( )
    {
        printf("Welcome to the Letter Guessing Game\n");
        printf("To begin you will enter the number of games you want to play(1 – 7 games)\n");
        printf("You have 5 chances to guess each letter\n");
        printf("Let's begin:\n\nHow many games would you like to play? (1 - 7):");
        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 GetUserLetter( )
    {
        char guess;
        printf("Enter a letter: ");
        scanf("%c",&guess);
        return guess;
    
    
    }
    
    
    //this function takes two arguments, the guess from the player and the answer 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 answer and returns a 0 if they do not match
    
    
    int CheckLetter(char userLetter, char correctLetter)
    {
        char value;
        if (userLetter > correctLetter)value=0;
        else value=1;
        printf("letter come before");
        return 0;
    }
    
    
    
    
    int SingleGame(char letter)
    {
    	int numGuesses = 0;
    	int winOrLose = 0; //SHOULD BE INITIALZED TO 0
        
    	while(numGuesses < MAXGUESSES && winOrLose == 0)
    	{
    		//GetUserLetter function call
            GetUserLetter();
    		
            //CheckLetter function call
            CheckLetter();
            
            //update counter for number of guesses used
    		numGuesses = numGuesses +1;
            
            
        }
    
    
    }
    Now I'm just getting one error. Too few arguments to function call.

    Code:
    //CheckLetter function call
            CheckLetter();

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Not what I said to do. I said remove the parameter types, not the parameters.

    As an example, this is a function that takes two int parameters and returns an int:
    Code:
    int myFunction(int a, int b)
    {
        return a + b;
    }
    you can call it like this:
    Code:
    int param1 = 3, param2 = 4;
    int result = myFunction(param1, param2);
    but you incorrectly tried to call it this way in your code:
    Code:
    int param1 = 3, param2 = 4;
    int result = myFunction(int param1, int param2);
    See the difference?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    Ahh okay I see what your saying. The code is running fine now but it's having trouble finding my file. Am I opening it or scanning it wrong?

    Code:
    //open file
        FILE *fpinp;
        
        fpinp=fopen("game.txt", "r");
    	
        //get number of games to play
        scanf(" %d", &gamesToPlay);
    	for(i=0;i<gamesToPlay;i++)
    	{
        
        //get a letter from file
            fscanf(fpinp, " %c", &letter);
                
            GetUserLetter();
    Thanks for all your help by the way!

  12. #12
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Your understanding of functions is a little bit wrong.
    First, you must allways declare how many arguments take your function.
    As example this:
    Code:
    void Instructions( );
    Empty parenthesis stay for no or more arguments. This is undefined behavior!
    Should the function take realy no arguments, than write it, like:
    Code:
    void Instructions(void);
    Second, if you need the returned value from a function, than you must take it.
    Code:
    GetUserLetter();
    This is, like you go to a condom automat, insert coins and go away without to take the condom.
    Have a nice night with your girl/boy-friend and few weeks letter, your girl/boy-friend are in undefinded behavior (and you too).
    So, this is better:
    Code:
    char userLetter;
    userLetter = GetUserLetter();
    Same here:
    Code:
    winOrLose = CheckLetter(userLetter, letter);
    Other points are:
    Please read your function 'CheckLetter' very carefully.
    They dont do what you expect.
    I suggest you to rewrite this funktion with the returned values:
    -1 = letter comes after (your letter is to low)
    0 = it is the letter
    1 = letter comes before(your letter is to high)
    This goes conformable with the functioncall above.

    Please check if the file was realy opened.
    You open the file correctly …
    Code:
    inp=fopen("game.txt", "r");
    … but you dont check, if its realy open.
    Check it with:
    Code:
    if (inp == NULL) { /* give out error message and quit */ }
    I dont know, why you edit this call:
    Code:
    //Play one game (Call PlayOneRound function)
            SingleGame(i);
    At the begining it was:
    Code:
    //Play one game (Call PlayOneRound function)
            int = SingleGame(char letter);
    Here you have give the funktion the argument 'letter', that was right (without type).
    Only the returned value should be assigned to the right variable.
    I think it should by:
    Code:
    //Play one game (Call PlayOneRound function)
            uWINuLOSE = SingleGame(letter);
    So, now I hope you have a little idea how functions work and how came the returned value back to the caller.
    Go through all your functions and read carefully what it those and read also all callings to it.
    Look what you give as arguments and what give it back.

    PS: sorry for my horrible engish, thats not my native language
    Last edited by WoodSTokk; 09-24-2014 at 06:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread