2015-02-12 19 views
0

我想通過使用Visual Studio 2012來做一個猜測C語言遊戲的字母,但是我不斷收到錯誤和警告,我不知道如何解決它們。我不斷收到的錯誤是:在C程序中猜字遊戲,困惑錯誤

1)警告1警告C4133: '功能':不兼容的類型 - 從 'FILE *' 到 '爲const char *'

2)警告2警告C4047: '=':'FILE *'與'int(__cdecl *)(FILE *)'的間接級別不同'

3)錯誤3錯誤C2449:在文件範圍發現'{'(缺少函數頭?

4.)錯誤4錯誤C1004:意外結束文件中找到

5.)智能感知:argum類型的 「FILE *」 ENT是與參數不相容

6.)智能感知:類型爲 「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; 
      } 
+0

「SingleGame」函數的其餘部分在哪裏? – PaulMcKenzie 2015-02-12 02:23:18

回答

1

1)警告1警告C4133: '功能':不兼容的類型 - 從 'FILE *' 到 '爲const char *'

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

如果你想從一個特定的FILE *閱讀,使用fscanf()

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

2)警告2警告C4047: '=': 'FILE *' 在間接的水平不同於 'INT(__cdecl *)(FILE *)'

infile = fclose; 

你想調用fclose()而不是將其分配給infile(其也沒有兼容型):

fclose(infile); 

3.)錯誤3錯誤C2449:發現「{」在文件範圍(缺少函數頭?)

int SingleGame(char file_letter); 

分號,使一個函數聲明/ protoype,但你要定義一個。刪除它。

這裏的分號是所謂的null statement)。這意味着如果兩個變量都相等,那麼什麼都不會做。

if(file_letter == RetrieveGuess); 
0

您在這裏有許多與您的代碼有關的問題。一次處理多個問題總是很困難。我的建議是將所有這些代碼複製到不同的文件中,並且一次只重建一行文件,並且在編譯當前文件錯誤和警告免費之後只添加另一行。

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