简体   繁体   English

C程序中的字母猜测游戏,错误混淆

[英]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. 我正在尝试使用Visual Studio 2012用C语言做一个猜字母游戏,但是我一直收到错误和警告,而且不知道如何解决它们。 The errors that I keep receiving are: 我不断收到的错误是:

1.)Warning 1 warning C4133: 'function' : incompatible types - from 'FILE *' to 'const char *' 1.)警告1警告C4133:'功能':不兼容的类型-从'FILE *'到'const char *'

2.)Warning 2 warning C4047: '=' : 'FILE *' differs in levels of indirection from 'int (__cdecl *)(FILE *)' 2.)警告2警告C4047:'=':'FILE *'与'int(__cdecl *)(FILE *)'的间接访问级别不同

3.)Error 3 error C2449: found '{' at file scope (missing function header?) 3.)错误3错误C2449:在文件范围内找到“ {”(缺少函数标头?)

4.)Error 4 error C1004: unexpected end-of-file found 4)错误4错误C1004:发现意外的文件结尾

5.)IntelliSense: argument of type "FILE *" is incompatible with parameter 5.)IntelliSense:“ FILE *”类型的参数与参数不兼容

6.)IntelliSense: a value of type "int (__cdecl *)(FILE *_File)" cannot be assigned to an entity of type "FILE *" 6.)IntelliSense:类型“ int(__cdecl *)(FILE * _File)”的值不能分配给“ 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 *' 1.)警告1警告C4133:'功能':不兼容的类型-从'FILE *'到'const char *'

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

If you want to read from a specific FILE * , use fscanf() : 如果要读取特定的FILE * ,请使用fscanf()

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

2.)Warning 2 warning C4047: '=' : 'FILE *' differs in levels of indirection from 'int (__cdecl *)(FILE *)' 2.)警告2警告C4047:'=':'FILE *'与'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 (它也没有兼容的类型):

fclose(infile);

3.)Error 3 error C2449: found '{' at file scope (missing function header?) 3.)错误3错误C2449:在文件范围内找到“ {”(缺少函数标头?)

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 ). 这里的分号是所谓的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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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