Thread: Help with guess the letter program

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    7

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It'd be much better if, instead of attaching everything to pdfs, had posted everything in this thread directly, and also given us an idea as to what you need help with...
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    7
    I was trying to do so, however I was not able to so that was the only way I could post it which took me awhile to even do that.

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    7
    Code:
    
    
    Also there are errors on:
    
    Line 30: 'lett': unreferenced local variable
    
    Line 65: found '{' at file scope (missing a header?) & expected a declaration 
    
    Line 71: Syntax error '}'
    
    Line 75: found '{' at file scope (missing a header?) & also expected a declaration
    
    Line 80: Syntax error '}' 
    
    Line 92: Syntax error '}'
    
    Line 111: Syntax error '}'
    Last edited by kd123; 02-20-2016 at 04:22 PM.

  5. #5
    Registered User
    Join Date
    Feb 2016
    Posts
    7
    Code:
    
    Topics: printf, scanf, file input, user defined function, and conditions (if/else, switch, loops)
    
    Instructions:
    •The program will then open a file named “letters.txt” and reads one letter per game
    •The letters.txt file will contain 10 randomly selected lowercase letters
    •A preprocessor directive must be used to define the maximum number of guesses as 4
    •The program will end after the selected number of games have been played along with the letter
    they were trying to guess
    •You must have the following 5 user-defined functions:
    
    THE FIRST TWO FUNCTIONS BELOW are called from inside the main function
    
    //this function provides instructions to the user on how to play the game
    void GuessItRules();
    
    //This function runs one game.
    //Input: character from the file
    //function returns 1 if the user wins the game and 0 if the user loses a game
    int PlayOneGame(char);
    
    ALL FUNCTIONS BELOW are called from inside the PlayOneGame function
    (above)
    
    //This function prompts the player to make a guess and returns that guess
    char GetTheLetter();
    
    //Input: character from file and character guess from user
    //Returns 1 if the guess matches the solution & returns 0 if they don’t match
    int IsAWinner(char, char);
    
    //Input:character from file and character guess from user
    //Prints a message if the guess comes alphabetically before, after,
    //or equal to the letter
    void CompareTheLetters(char, char);
    
    
    Additional Requirements:
    •Use function prototypes
    •Write comments for each function prototype
    •Write comment for each function definition
    •Place adequate comments throughout your code
    •Use meaningful variable names
     
    The exact errors I got: 
    •   Line 29- 'win' unreferenced local variable
    •   Line 65- Expected a declaration 
    •   Line 66- found '{' at file scope (missing a header?)
    •   Line 76- found '{' at file scope (missing a header?)
    •   78- syntax error. '}'
    •   86- Expected a declaration 
    •   97- Expected a declaration

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Okay, let me help you then, this is your code:
    Code:
    //precompiler directives
    #define _CRT_SECURE_NO_WARNINGS // to void scanf warning or error
    #include <stdio.h> //printf, scanf definitions
    #include "guessit.h"
    #define MAXGUESSES 4
    
    //This functionprovides instructions to the user on how to play the game
    void GuessItRules();
    //This functionruns one game.
    //Input: character from the file
    //function returns 1 if the user wins the game and 0 if the user loses a game
    int PlayOneGame(char letter);
    //This function prompts the player to make a guess and returns that guess
    char GetUsersLetter();
    //Input: character from file and character guess from user
    //returns 1 if the guess matches the solution & returns 0 if they don’t match
    int IsAWinner(char YouWin, char YouLose);
    //Input: character from file and character guess from user
    //Prints a message if the guess comes alphabetically before, after,
    //or equal to the letter
    void CompareLetters(char usersLetter, char rightLetter);
    
    int main(void)
    {
        //declare variables
        int numOfgames;
        int i;
        char letter;
        int win;
        int lett;
        int YouWinOrYouLose;
        char solution;
        //instructions
        GuessItRules();
        //open the file
        FILE * fpinp;
        fpinp = fopen("letters.txt", "r");
    
        //get the number of games to play
        scanf("%d", &numOfgames);
        for (i = 0; i < numOfgames; i++)
        {
            //get letter from file
            fscanf(fpinp, "%c", &letter);
            GetTheLetter();
            //play one game (Call PlayOneGame function)
            YouWinOrYouLose = PlayOneGame(letter);
            //check if you win or you lose
            if(YouWinOrYouLose == 1)
            {
                printf("Great job! You win!");
            }
            else if(YouWinOrYouLose == 0)
            {
                printf("You did not guess the correct 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 GuessItRules();
    {
        printf("Welcome to the Guess the Letter game!\n");
        printf("First enter the number of games you would like to play you can pick anywhere from 1 to 5 games\n");
        printf("You have only 4 chances to guess the correct letter\n");
        printf("Now let begin the game:\n\n");
        return();
    }
    
    //This function prompts the player to make a guess and returns that guess
    char GetUsersLetter();
    {
        char guess;
        printf("Enter a letter: ");
        scanf("%c", &guess);
        return guess;
    }
    
    //Input: character from file and character guess from user
    //Prints a message if the guess comes alphabetically before, after,
    //or equal to the letter
    void CompareLetters(char usersLetter, char rightLetter);
    {
        char value;
        if (userLetter > rightLetter) value = 0;
        else value = 1;
        printf("The letter comes before");
        return(0);
    }
    
    //Function definitions
    //keep the comments from the assignment
    int PlayOneGame(char letter);
    {
        int numGuesses;
        int winOrlose; //should be initialized to 0
        char usersGuess;
    
        while (numGuesses < MAXGUESSES && winOrlose == 0)
        {
            //GetUsersLetter function call
            GetUsersLetter();  //CompareLetters function call
            CompareLetters(char usersLetter, char rightLetter);
            //update the counter for number of guesses that were used
            numGuesses = numGuesses + 1;
        }
    }
    Last edited by GReaper; 02-20-2016 at 05:00 PM.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Feb 2016
    Posts
    7

    Program

    Code:
    1. //precompiler directives
    2. #define _CRT_SECURE_NO_WARNINGS // to void scanf warning       or error
    3. #include <stdio.h> //printf, scanf definitions
    4. #include "guessit.h"
    5. #define MAXGUESSES 4
    6.
    7. //This function provides instructions to the user on how to play      the game
    8. void GuessItRules();
    9. //This function runs one game.
    10. //Input: character from the file
    11. //function returns 1 if the user wins the game and 0 if the user     loses a game
    12. int PlayOneGame(char letter);
    13. //This function prompts the player to make a guess and returns      that guess
    14. char GetUsersLetter();
    15. //Input: character from file and character guess from user
    16. //returns 1 if the guess matches the solution & returns 0 if they      don’t match
    17. int IsAWinner(char YouWin, char YouLose);
    18. //Input: character from file and character guess from user
    19. //Prints a message if the guess comes alphabetically before,          after,
    20. //or equal to the letter
    21. void CompareLetters(char usersLetter, char rightLetter);
    22.
    23. int main(void)
    24. {
    25.       //declare variables
    26.       int numOfgames;
    27.       int i;
    28.       char letter;
    29.       int win;
    30.       int lett;
    31.       int YouWinOrYouLose;
    32.       char solution;
    33.       //instructions
    34.       GuessItRules();
    35.       //open the file
    36.       FILE * fpinp;
    37.       fpinp = fopen("letters.txt", "r");
    38.
    39.       //get the number of games to play
    40.       scanf("%d", &numOfgames);
    41.       for (i = 0; i < numOfgames; i++)
    42.       {
    43.           //get letter from file
    44.           fscanf(fpinp, "%c", &letter);
    45.           int GetTheLetter();
    46.           //play one game (Call PlayOneGame function)
    47.           YouWinOrYouLose = PlayOneGame(letter);
    48.           //check if you win or you lose
    49.           if(YouWinOrYouLose == 1)
    50.           {
    51.                 printf("Great job! You win!");
    52.           }
    53.           else if(YouWinOrYouLose == 0)
    54.           {
    55.                 printf("You did not guess the correct letter.\nThe                          letter you were looking for was %c.\n\n", solution);
    56.           }
    57. }
    58.     //close file
    59.     fclose(inp);
    60.     return (0);
    61. }
    62.
    63. //This function provides instructions to the user on how to play the game
    64. void GuessItRules();
    65. {
    66.     printf("Welcome to the Guess the Letter game!\n");
    67.     printf("First enter the number of games you would like to              play you can pick anywhere from 1 to 5 games\n");
    68.     printf("You have only 4 chances to guess the correct                      letter\n");
    69.     printf("Now let begin the game:\n\n");
    70.     return();
    71. }
    72.
    73. //This function prompts the player to make a guess and returns      that guess
    74. char GetUsersLetter();
    75. {
    76.     char guess;
    77.     printf("Enter a letter: ");
    78.     scanf("%c", &guess);
    79.     return guess;
    80. }
    81.
    82. //Input: character from file and character guess from user
    83. //Prints a message if the guess comes alphabetically before,          after,
    84. //or equal to the letter
    85. void CompareLetters(char usersLetter, char rightLetter);
    86. {
    87.     char value;
    88.     if (userLetter > rightLetter) value = 0;
    89.     else value = 1;
    90.     printf("The letter comes before");
    91.     return(0);
    92. }
    93.
    94. //Function definitions
    95. //keep the comments from the assignment
    96. int PlayOneGame(char letter);
    97. {
    98.     int numGuesses;
    99.     int winOrlose; //should be initialized to 0
    100.         char usersGuess;
    101.
    102.         while (numGuesses < MAXGUESSES && winOrlose == 0)
    103.         {
    104.               //GetUsersLetter function call
    105.               GetUsersLetter();
    106.               //CompareLetters function call
    107.               CompareLetters(char usersLetter, char rightLetter);
    108.               //update the counter for number of guesses                                  that were used
    109.               numGuesses = numGuesses + 1;
    110.         }
    111. }

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You mustn't have semicolons when defining the functions( below main ). Also, void functions shouldn't return anything.
    You should look up how functions are called. A simple copy-paste like that on line 106-107 isn't enough...

    Btw, this is C, not C++.
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Feb 2016
    Posts
    7
    Ok and what do I do to fix my errors that I received? Also where should this part be
    //Input: character from file and character guess from user
    //Returns 1 if the guess matches the solution & returns 0 if they don’t match
    int IsAWinner(char, char);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 10-20-2011, 06:32 PM
  2. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  3. Guess the number program help needed
    By msbrownsugah in forum C Programming
    Replies: 7
    Last Post: 05-06-2009, 05:58 PM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. Guess what this 5.5kb program does.
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2006, 02:31 PM

Tags for this Thread