Thread: Help on game code

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    10

    Help on game code

    For this code im getting some errors that i can't figure out why its failing to compile. most of the errors are on line 37 85 and 101. Can someone please help figure out what im doing wrong?

    Code:
    #define_CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<ctype.h>
    
    
    #defineMAXGUESSES 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 correct guess.
    //It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
    int PlayGuess(char solution);
    
    
    //this function prompts the player to make a guess and returns that guess
    //this function is called from inside the PlayGuess( ) function described above
    char  GetLetter();
    
    
    //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 CompareLetters(char guess, char solution);
    
    
    
    
    
    
    
    
    int main()
    {
    	int i = 0;
    	int numgames = 0;
    	char solution;
    	char guess;
    	int compareletter(char guess, char solution);
    	FILE *inp;
    	inp = fopen_s("inputLet.txt", "r");
    	fscanf(inp, "%c", &solution);
    	Instructions();
    //get number of games the user wants to play
    	printf("Please enter the number of games you want to play\n");
    	scanf("%d", &numgames);
    	for (i = 1; i <= numgames; i++)
    //print current game (value of i)
    	{
    //get letter to guess from file
    		fscanf(inp, "%c", &solution);
    		PlayGuess(solution);
    		printf("\nThe letter is %c\n", solution);
    	}
    
    
    
    
    	fclose(inp);
    
    
    }
    
    
    void Instructions()
    {
    	printf("Welcome to Letter Guess\n");
    	printf("To begin you will enter the number of games you want to play(1  4 games)\n");
    	printf("You have 5 chances to guess each letter\n");
    	printf("Let's begin\n");
    
    
    }
    
    
    
    
    
    
    
    
    
    
    int PlayGuess(charsolution) //player defined guesses.
    {
    
    
    //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;
    //declare additional variables 1. number of guesses 2. Win or Lose
    int numGuess = 0; 
    int WorL = 0;     
    		while (numGuesses < MAXGUESSES && win == 0) {
    //get a guess from the user  by calling the GetTheGuess function
    			GetTheGuess();
    //change the guess to lowercase
    			int letter = tolower(solution);
    //win = call the function to compare the guess with the solution
    			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
    	}
    
    
    //this function provides instructions to the user on how to play the game 
    
    
    	void GameRules(); {
    		printf("Welcome to the letter guessing game!");
    		printf("\nYou have the options to play 1-8 rounds of this game.");
    		printf("\nTo begin, you will have a total of 5 guesses per letter in every game.\n");
    
    
    return;
    	}
    }
    
    
    
    
    //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 GetLetter() {
    	char guess;
    	printf("Enter a letter as your guess: \n");
    	scanf("\n%c", &guess);
    	printf("The letter you chose is %c\n", guess);
    	return guess;
    }
    
    
    
    
    //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 guess, char solution) {
    	char value;
    	if (guess > solution)value = 0;
    	else value = 1;
    	printf("");
    
    
    return 0;
    }
    
    

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    90
    Hey,

    At the beginning of the main when declaring variables you have a function declaration (compareLetters), but you are never calling the function in the program if I'm not mistaken.
    Also, have you noticed that the function does not return value but always returns 0?
    On top of that, note that compareLetters != CompareLetters.

    Your identation is all over the place, the code is unreadable. Fix it.

    Maybe someone with a bit more time can help you out, It would be helpful for you to post the errors you are getting so users can help you troubleshoot the problems.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,730
    There needs to be a space between #define and macro names, as well as between parameter types and names.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    10
    errors found

    too few arguments in function call line 37
    'function': "FILE**' differs in levels of indirection from char [15]' line 38
    'fopen_s': different types for formal and actual parameter 1 line 38
    'fopen_s': too few arguments for call line 38
    '=':'FILE*' differs in levels of indirection from 'errno_t' line 38
    'GetTheGuess' undefined; assuming extern returning int line 86
    'PlayGuess' must return a value line 102

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    10

    basic program help

    Heres an update to the code. Having trouble with compare letter function.
    Code:
    
    
    
    
    #define_CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<ctype.h>
    #defineMAXGUESSES 5
    
    
    //this function provides instructions to the user on how to play the game 
    
    
    void GameRules();
    
    
    //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);
    
    
    //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(char1, char2);
    
    
    int main() {
    
    
    //declare additional variables-Declare pointer-letter from file
        FILE * inPtr;
        int numGames = 0, i = 0;
        char letter = 0;
        int WorL = 0;
        char solution;
        char guess;
    
    
    //display game rules and ask for the number of games to play. Connect the file with fopen. 
        GameRules();
        inPtr = fopen("letterList.txt", "r");
        printf("How many games would you like to play?\n");
        scanf(" %d", &numGames);
    
    
    //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 while making the letters lowercase and print letter.
            GetTheGuess();
            fscanf(inPtr, "%c", &solution);
            letter = tolower(solution);
    //call the GuessTheLetter function and pass it the solution
            if (WorL == 1) {
                printf("Ayyy there you go! You go Glen Coco!");
            }
            else if (WorL == 0); {
                printf("Sorry try again...\n");
                printf("The letter you were looking for is %c", &solution);
            }
    
    
        }
    
    
    //close file pointer
        fclose(inPtr);
    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;
    //declare additional variables 1. number of guesses 2. Win or Lose
    int numGuess = 0; //ask prof why the =0 was needed to take away an error when building. 
    int WorL = 0; //ask prof why the =0 was needed to take away an error when building. 
        while (numGuesses < MAXGUESSES && win == 0) {
    //get a guess from the user  by calling the GetTheGuess function
            GetTheGuess();
    //change the guess to lowercase
            int letter = tolower(solution);
    //win = call the function to compare the guess with the solution
            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
    }
    
    
    //this function provides instructions to the user on how to play the game 
    
    
    void GameRules() {
        printf("Welcome to the letter guessing game!");
        printf("\nYou have the options to play 1-8 rounds of this game.");
        printf("\nTo begin, you will have a total of 5 guesses per letter in every game.\n");
    
    
    return;
    }
    
    
    //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 guess;
        printf("Enter a letter as your guess: \n");
        scanf("\n%c", &guess);
        printf("The letter you chose is %c\n", guess);
        return guess;
    }
    
    
    //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(char1, char2) {
        char value;
        if (char1 > char2) value = 0;
        else value = 1;
        printf("");
    
    
    return 0;
    }
    

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    userx@slackwhere:~/bin
    $ gcc game.c
    
    //define and what is defined touching both of them
    
    game.c:1:2: error: invalid preprocessing directive #define_CRT_SECURE_NO_WARNINGS
     #define_CRT_SECURE_NO_WARNINGS
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    game.c:6:2: error: invalid preprocessing directive #defineMAXGUESSES
     #defineMAXGUESSES 5
      ^~~~~~~~~~~~~~~~
    //changed to fopen, Linux stdio.h does not know what that is either, but,
    //if you see implicit declaration then it needs the header
    // file to define that function in your include statements.
    
    game.c: In function ‘main’:
    game.c:48:8: warning: implicit declaration of function ‘fopen_s’; did you mean ‘fopen’? [-Wimplicit-function-declaration]
      inp = fopen_s("inputLet.txt", "r");
            ^~~~~~~
            fopen
    game.c:48:6: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
      inp = fopen_s("inputLet.txt", "r");
          ^
    
    // PlayGuess is declared an int return but has no return value in function;
    
    game.c: In function ‘PlayGuess’:
    game.c:91:5: warning: type of ‘charsolution’ defaults to ‘int’ [-Wimplicit-int]
     int PlayGuess(charsolution) //player defined guesses.
         ^~~~~~~~~
    game.c:91:5: error: argument ‘charsolution’ doesn’t match prototype
    game.c:19:5: error: prototype declaration
     int PlayGuess(char solution);
         ^~~~~~~~~
    
    //not showing as declared due to your mistake declaring it
    
    game.c:105:23: error: ‘MAXGUESSES’ undeclared (first use in this function)
       while (numGuesses < MAXGUESSES && win == 0) {
                           ^~~~~~~~~~
    
    // you have NO function in this file called GetTheGuess, other then you 
    // calling it. not even a header file to show it where it is. 
    
    game.c:105:23: note: each undeclared identifier is reported only once for each function it appears in
    game.c:107:4: warning: implicit declaration of function ‘GetTheGuess’ [-Wimplicit-function-declaration]
        GetTheGuess();
        ^~~~~~~~~~~
    
    // function wanting to use this solution is not passed in by function.
    // or you are not understadnig functions. your pram uses the word
    // charsolution being passed in, and your checking for solution instead,
    //maybe that is another oops I forgot to hit the space bar mistake. ;) 
    
    game.c:109:25: error: ‘solution’ undeclared (first use in this function); did you mean ‘charsolution’?
        int letter = tolower(solution);
                             ^~~~~~~~
                             charsolution
    // as stated above
    
    game.c:126:1: warning: ‘return’ with no value, in function returning non-void
     return;
     ^~~~~~
    game.c:91:5: note: declared here
     int PlayGuess(charsolution) //player defined guesses.
         ^~~~~~~~~
    userx@slackwhere:~/bin
    Last edited by userxbw; 10-15-2017 at 09:42 PM.

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by carancho View Post
    Heres an update to the code. Having trouble with compare letter function.
    you need to read up on how to write a function; and how to compare chars
    Code:
    $ gcc game.c
    game.c:4:2: error: invalid preprocessing directive #define_CRT_SECURE_NO_WARNINGS
     #define_CRT_SECURE_NO_WARNINGS
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    game.c:7:2: error: invalid preprocessing directive #defineMAXGUESSES
     #defineMAXGUESSES 5
      ^~~~~~~~~~~~~~~~
    game.c:38:1: warning: parameter names (without types) in function declaration
     int CompareLetters(char1, char2);
     ^~~
    game.c: In function ‘GuessTheLetter’:
    game.c:96:25: error: ‘MAXGUESSES’ undeclared (first use in this function)
         while (numGuesses < MAXGUESSES && win == 0) {
                             ^~~~~~~~~~
    game.c:96:25: note: each undeclared identifier is reported only once for each function it appears in
    game.c: In function ‘CompareLetters’:
    game.c:140:5: warning: type of ‘char1’ defaults to ‘int’ [-Wimplicit-int]
     int CompareLetters(char1, char2) {
         ^~~~~~~~~~~~~~
    game.c:140:5: warning: type of ‘char2’ defaults to ‘int’ [-Wimplicit-int]
    and go back and look at post #6 addressing the other errors that are still in your code.

    functions:
    Code:
     int MyFunction(int  1, int  2)
    {
      if ( 1 == 2)
       return 0;
    else
     return -1;
    }
    
    int main(void)
    {
      int val1 = 43, val2 =44;
    
    if ( MyFunction(val1, val2) == 0 )
    do something
    else
    do somethingElse
    
    return 0;
    }
    this is where I am at with your program
    Code:
    $ 
    userx@slackwhere:~/bin
    $ ./a.out
    Welcome to the letter guessing game!
    You have the options to play 1-8 rounds of this game.
    To begin, you will have a total of 5 guesses per letter in every game.
    How many games would you like to play?
    4
    Enter a letter as your guess: 
    e
    The letter you chose is e
    Segmentation fault
    I do not have a copy of your input file. "letterList.txt", so that is as far as I am getting.
    Last edited by userxbw; 10-15-2017 at 10:07 PM.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    ok I got this far
    Code:
    userx@slackwhere:~/bin
    $ ./a.out
    Welcome to the letter guessing game!
    You have the options to play 1-8 rounds of this game.
    To begin, you will have a total of 5 guesses per letter in every game.
    How many games would you like to play?
    1
    Enter a letter as your guess: 
    c
    The letter you chose is c
    Sorry try again...
    The letter you were looking for is #
    the data file was
    Code:
    a
    s
    d
    c
    v
    b
    n
    m
    q
    w
    e
    r
    t
    y
    Code:
    int CompareLetters(char char1,char  char2) {
       // char value;
        if (char1 > char2) 
          return 0; //value = 0;
        else 
          return 1; //value = 1;
        //printf("");
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Business code' vs 'Game code'
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-09-2007, 03:33 AM
  2. c++ game [code] need help
    By mario fortunato in forum Game Programming
    Replies: 3
    Last Post: 02-24-2006, 08:41 AM
  3. I need a code for Tic Tac Toe game ?
    By martyr in forum C++ Programming
    Replies: 11
    Last Post: 12-07-2003, 03:29 AM
  4. Tic-Tac-Toe game Code???
    By IVIr BigTime in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2002, 05:18 PM
  5. game code
    By The WaRped OnE in forum Game Programming
    Replies: 1
    Last Post: 02-28-2002, 08:46 PM

Tags for this Thread