gpt4 book ai didi

c - C 程序中的字母猜谜游戏,错误混淆

转载 作者:行者123 更新时间:2023-11-30 15:24:19 26 4
gpt4 key购买 nike

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

1.)警告 1 警告 C4133:“function”:不兼容的类型 - 从“FILE *”到“const char *”

2.)警告 2 警告 C4047:“=”:“FILE *”与“int (__cdecl *)(FILE *)”的间接级别不同

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

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

5.)IntelliSense:“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;
}

最佳答案

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

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

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

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

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

infile = fclose;

您想要调用fclose(),而不是将其分配给infile(它也没有兼容的类型):

fclose(infile);

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

int SingleGame(char file_letter);

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

这里的分号是所谓的空语句)。这意味着如果两个变量相等,则不会执行任何操作。

if(file_letter == RetrieveGuess);

关于c - C 程序中的字母猜谜游戏,错误混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28468079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com