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

时间:2015-02-12 02:08:39

标签: c

我正在尝试使用Visual Studio 2012在C语言中进行猜字游戏,但我不断收到错误和警告,我不知道如何修复它们。我一直收到的错误是:

1.)警告1警告C4133:'功能' :不兼容的类型 - 来自' FILE *'到#char; char *'

2.)警告2警告C4047:' =' :' FILE *'间接层次与' int(__ cdecl *)(FILE *)'

3.)错误3错误C2449:找到' {'在文件范围(缺少函数头?)

4.)错误4错误C1004:发现意外的文件结尾

5.)智能感知:类型" FILE *"的参数与参数

不兼容

6.)IntelliSense:类型" int(__ cdecl *)(FILE * _File)"不能分配给" FILE *"类型的实体。

我也看到了错误,表示期待声明。' 每当我尝试解决问题时,我最终都会在其他方面造成更多问题。有人可以帮我这个吗?谢谢!

这是我的代码:

#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;
            }

3 个答案:

答案 0 :(得分:1)

  

1.)警告1警告C4133:&#39;功能&#39; :不兼容的类型 - 来自&#39; FILE *&#39; to&#39; const char *&#39;

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

如果您想阅读特定的FILE *,请使用fscanf()

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

2.)警告2警告C4047:&#39; =&#39; :&#39; FILE *&#39;间接水平与&#39; int(__ cdecl *)(FILE *)&#39;

不同
infile = fclose;

您想致电fclose()而不是将其分配给infile(它也没有兼容类型):

fclose(infile);
  

3.)错误3错误C2449:找到&#39; {&#39;在文件范围(缺少函数头?)

int SingleGame(char file_letter);

分号使其成为函数声明/原型,但您想要定义一个。删除它。

这里的分号是所谓的null statement)。这意味着如果两个变量都相等,那么就不会做任何事情。

if(file_letter == RetrieveGuess);

答案 1 :(得分:0)

您的代码存在许多问题。一次处理多个问题总是很困难。我的建议是将所有这些代码复制到另一个文件中,并一次重建一行该文件,并在编译当前文件错误和警告后再添加另一行。

答案 2 :(得分:0)

代码中有很多语法错误。我已经为你纠正过了。虽然逻辑上不确定代码是否正确。你得跑去看看。

#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;
        }
    }
}