繁体   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