简体   繁体   中英

Letter guessing game in C program, confused of errors

I am trying to do a letter guessing game in C language by using Visual Studio 2012, but I keep getting errors and warnings and I have no clue how to fix them. The errors that I keep receiving are:

1.)Warning 1 warning C4133: 'function' : incompatible types - from 'FILE *' to 'const char *'

2.)Warning 2 warning C4047: '=' : 'FILE *' differs in levels of indirection from 'int (__cdecl *)(FILE *)'

3.)Error 3 error C2449: found '{' at file scope (missing function header?)

4.)Error 4 error C1004: unexpected end-of-file found

5.)IntelliSense: argument of type "FILE *" is incompatible with parameter

6.)IntelliSense: a value of type "int (__cdecl *)(FILE *_File)" cannot be assigned to an entity of type "FILE *"

I also see errors that say 'expected a declaration.' Every time I try to fix things, I end up causing more issues in other areas. Could someone give me assistance with this? Thank you!

Here is my code:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define MAXGUESSES 5

int SingleGame(char file_letter);

int main()
{
    //declare additional variables
int PlayGames = 4,
i = 0;
FILE * infile;
char letter;
    //display instructions
    printf("Welcome to the Letter Guessing Game!\n");
    printf("You will enter the number of games that you want to play, which is 1-4 games\n");
    printf("You have 5 chances to guess each letter\n");
    printf("Let's begin!\n");

    //open file
    infile = fopen("lettersin.txt", "r");

    //get number of games to play
    printf("How many games would you like to play?(1-4)\n");
    scanf("%d", &PlayGames);

    for(i=0;i<PlayGames;i++)
    {
        //get a letter from file
        scanf(infile, " %c", &letter);

        //Play one game
        printf("Let's play a game %d\n", i);

        //check for win or lose
        SingleGame (letter);
    }

    //close file
    infile = fclose;
    return 0;
}
int SingleGame(char file_letter);
{

//Function definitions
    int numGuesses = 0;
    while(numGuesses < MAXGUESSES);
    char RetrieveGuess = 0;
    int PlayGames = 0;

    {
        printf("Enter a guess\n");
        scanf("%c" , &RetrieveGuess);
        if(file_letter == RetrieveGuess);
        {
            printf("You guessed it!\n");
        }
        else
        {
            if(file_letter>RetrieveGuess)
            {
                printf("The letter you are trying to guess comes before:%d\n",RetrieveGuess)
            }
            {
            else if(file_letter<RetrieveGuess)
            {
                printf("The letter you are trying to guess comes after:%d\n", RetrieveGuess)
            }


            {
            numGuesses = numGuesses +1;
            }

1.)Warning 1 warning C4133: 'function' : incompatible types - from 'FILE *' to 'const char *'

scanf(infile, " %c", &letter);

If you want to read from a specific FILE * , use fscanf() :

fscanf(infile, " %c", &letter);

2.)Warning 2 warning C4047: '=' : 'FILE *' differs in levels of indirection from 'int (__cdecl *)(FILE *)'

infile = fclose;

You want to call fclose() and not assign it to infile (which also doesn't have a compatible type):

fclose(infile);

3.)Error 3 error C2449: found '{' at file scope (missing function header?)

int SingleGame(char file_letter);

The semicolon makes that a function declaration/protoype, but you want to define one. Delete it.

The semicolon here is a so-called null statement ). This means if both variables are equal, then nothing will be done.

if(file_letter == RetrieveGuess);

You have a number of issues with your code here. It is always difficult to work on more than one problem at a time. My advice is that you copy all this code into a different file, and rebuild this file one line at a time and only add another line after you compile the current file error and warning free.

Lots of syntax errors were in code. I've corrected them for you. Although not sure logically the code is correct or not. You got to run and see.

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define MAXGUESSES 5

void SingleGame(char file_letter);

int main()
{
    //declare additional variables
    int PlayGames = 4,
    i = 0;
    FILE* infile;
    char letter;
    //display instructions
    printf("Welcome to the Letter Guessing Game!\n");
    printf("You will enter the number of games that you want to play, which is 1-4 games\n");
    printf("You have 5 chances to guess each letter\n");
    printf("Let's begin!\n");

    //open file
    infile = fopen("lettersin.txt", "r");

    //get number of games to play
    printf("How many games would you like to play?(1-4)\n");
    scanf("%d", &PlayGames);

    for(i=0;i<PlayGames;i++)
    {
        //get a letter from file
        fscanf(infile, " %c", &letter);

        //Play one game
        printf("Let's play a game %d\n", i);

        //check for win or lose
        SingleGame (letter);
    }

    //close file
    fclose(infile);
    return 0;
}

void SingleGame(char file_letter)
{
//Function definitions
    int numGuesses = 0;
    while(numGuesses < MAXGUESSES)
    {
        char RetrieveGuess = 0;
        int PlayGames = 0;

        printf("Enter a guess\n");
        scanf("%c" , &RetrieveGuess);
        if(file_letter == RetrieveGuess)
        {
            printf("You guessed it!\n");
        }
        else
        {
            if(file_letter>RetrieveGuess)
            {
                printf("The letter you are trying to guess comes before:%d\n",RetrieveGuess);
            }
            else if(file_letter<RetrieveGuess)
            {
                printf("The letter you are trying to guess comes after:%d\n", RetrieveGuess);
            }

            numGuesses = numGuesses +1;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM