簡體   English   中英

我該如何解決C中的這些錯誤?

[英]how can i fix these errors in c?

我不斷收到這些錯誤。 我試圖制作像游戲一樣的掃雷器。

well.c: In function 'main':
well.c:170: warning: passing argument 1 of 'bombCheck' makes pointer from integer without a cast
well.c:170: warning: passing argument 3 of 'bombCheck' makes integer from pointer without a cast
well.c: In function 'fillGameBoard':
well.c:196: error: expected declaration or statement at end of input
#include <stdio.h>
#include <stdlib.h>

#define Rows 5
#define Columns 5
#define Bombs 5

void introduction(void)

{
        puts("Welcome to the minefield!");
        puts("In this level 2 game, you will win by choosing.");
        puts("all of the viable wells and not any of the.");
        puts("tool breaker spaces. In both games, there are.");
        puts("20 viable spaces and 5 tool breakers!");
        puts("Have fun and good luck!");

}

void fillGameBoard(char gameBoard[][Columns])
{

        int     i, j;
        FILE    *inputFile;
        char    gameDataFileName[30];
        int     yes = 0;


        do
        {
                printf("choose your spot");
                printf("\nfield1                field2\n");
                scanf(" %s",&gameDataFileName);

               if ((inputFile = fopen(gameDataFileName,"r")) == NULL)
               {
                       puts("\nWrong input! Try again!");
                       puts("check spelling, spacing, etc. make it exact!");
               }
               else
               {
                       yes = 1;
               }

       } while (yes == 0);

        for (i=0; i<Rows; i++)
        {

                for (j=0; j<Columns; j++)
                {

                        fscanf(inputFile, " %c", &gameBoard[i][j]);
        }
        fclose(inputFile);
return;
}

void fillUserBoard(char userBoard[][Columns])
{

        int i,j;                // counters

        for (i=0; i<Rows; i++)
        {
                for (j=0; j<Columns; j++)
                {
                        userBoard[i][j] = '~';
                }

        }

return;
}

void displayBoard(char board[][Columns])
{

        int i, j;

        printf("\n   ");
        for (i = 1; i <= Rows; i++)
        {

                printf("%d ",i+5);

        }

        puts("");
        for (i = 0; i <=Rows; i++)
        {

                printf("__");

        }

        puts("");
        for (i=0; i<Rows; i++)
        {

                printf("%d|",(i+1));

                for (j=0; j<Columns; j++)
                {
                        printf(" %c", board[i][j]);
                }
        puts("");

        }

return;
}

char bombCheck (char board[][Columns], int a, int b)
{

        char    gameOver;

        if (board[a][b] == '*')
        {

                puts("");
                puts("               BOOM");
                puts("You hit a mine.");
                puts("you are deaded.\n");
                puts("                      GAME OVER!!\n");
                gameOver =  'y';

        }

        else
        {

                gameOver = 'n';

        }

return gameOver;
}

int main (void)
{
        char    gameBoard[Columns][Rows];
        char    userBoard[Columns][Rows];
        char    done;
        char    win;
        char    gameOver;
        int     count;
        int     i;
        int     col;
        int     row;
        introduction();

        do
        {
                done=win='n';
                count=0;
                fillGameBoard(gameBoard);
                fillUserBoard(gameBoard);
                displayboard(userBoard);
                bombcheck();
                do
                {
                        displayBoard(userBoard);
                        printf("choose your column, numbered 1-5\n");
                        scanf(" %i", &col);
                        printf("choose your row, numbered 1-5\n");
                        scanf(" %i", &row);
                        done = bombCheck(col, row, gameBoard);
                                if (done='n')
                                {
                                         count+1;
                                                if (count==((Columns*Rows)-Bombs))
                                                {
                                                        printf("you win!\n");

                                                        done='y';
                                                }
                                                        else
                                                        {

                                                                done='n';


                                                userBoard[col][row]=gameBoard[col][row];
                                                        }
                                }
                } while (done != 'y');

                printf("do you want to play again? y/n \n");
                scanf(" %c", win);
        }while (win != 'y');

        return 0;
}
  1. 您在fillGameBoard()缺少括號。

     for (i=0; i<Rows; i++) { for (j=0; j<Columns; j++) { fscanf(inputFile, " %c", &gameBoard[i][j]); } /* Note closing brace! */ } fclose(inputFile); 
  2. 您正在以錯誤的順序將參數傳遞給bombCheck()

     /* Declared: char bombCheck (char board[][Columns], int a, int b) */ done = bombCheck(gameBoard, col, row); 
  3. 沒有參數的bombcheck()調用是什么? 請注意, bombcheck()bombCheck()不同。 C編程語言區分大小寫。

為了將來參考,請僅張貼與您的問題相關的最小代碼段,而不是整個程序。

C. bombcheck大小寫問題與bombCheck

參數順序很重要。 您已經使用(board, a, b)聲明了bombCheck ,但是使用(col, row, board)對其進行了調用。

獲取數組的地址是多余的。 scanf("%s",gameDataFileName);不需要& scanf("%s",gameDataFileName);

%s scanf是非常不安全的。 注意如果您輸入的字符超過30個(可能更多),會發生什么情況。 嘗試使用fgets

您在fillGameBoard缺少一個fillGameBoard括號(可能在第二個內部for循環中)。

您的縮進在某些地方是不一致的,特別是在您保留對齊的return語句(以及在塊末尾的其他一些語句)的地方。

總的來說,這是一個不錯的初學者程序。 堅持下去,您將立即熟悉那些編譯器錯誤!

缺少結束括號}以結束fillGameBoard函數。

開括號和閉括號的數量不累加。

第170行:參數順序錯誤

第51行:缺少}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM