how can i fix these errors in c?
I keep getting these errors. Im trying to make a mine sweeper like game.
well.c: In function 'main': well.c:170: warning: passing argument 1 of 'bombCheck' makes pointer 开发者_JAVA技巧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;
}
You're missing a brace in
fillGameBoard()
.for (i=0; i<Rows; i++) { for (j=0; j<Columns; j++) { fscanf(inputFile, " %c", &gameBoard[i][j]); } /* Note closing brace! */ } fclose(inputFile);
You're passing the arguments to
bombCheck()
in the wrong order./* Declared: char bombCheck (char board[][Columns], int a, int b) */ done = bombCheck(gameBoard, col, row);
What's with the
bombcheck()
call with no arguments? Note thatbombcheck()
is different frombombCheck()
. The C programming language is case-sensitive.
For future reference, post only the minimal code snippets relevant to your question, instead of an entire program.
Case matters in C. bombcheck
is not the same as bombCheck
.
Argument order matters. You have declared bombCheck
with (board, a, b)
but are calling it with (col, row, board)
.
Taking the address of an array is redundant. The &
is not necessary in scanf("%s",gameDataFileName);
scanf
with %s
is pretty unsafe. Watch what happens if you type in more than 30 characters (perhaps substantially more). Try fgets
instead.
You're missing a closing brace in fillGameBoard
(probably in the second inner for
loop).
Your indenting is inconsistent in some places, particularly where you have left aligned return
statements (and some other statements at the end of blocks).
Overall, this is a pretty good beginner program. Keep at it, and you'll get familiar with those compiler errors in no time!
There is a missing closing brace }
to end the fillGameBoard
function.
The number of opening and closing braces don't add up.
line 170: wrong order of arguments
line 51: missing }
精彩评论