Please help with this: error expected declaration or statement at end of input

This is a discussion on Please help with this: error expected declaration or statement at end of input within the C Programming forums, part of the General Programming Boards category; Solved I've googled this problem and it seems people who had it messed up their curly braces, but I've checked ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    6

    Please help with this: error expected declaration or statement at end of input

    Solved
    I've googled this problem and it seems people who had it messed up their curly braces, but I've checked a couple times and couldn't find anything wrong with my code. Maybe someone can spot something I didn't see or maybe it has nothing to do with braces in my case.

    I was trying to make a text based minesweeper, the error is in the last function (all the way at the bottom)
    It probably has other errors too since I wrote it fast but I'm just concerned with this one since I can't find the other errors until I fix this one

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MINE 9
    #define LOSE 11285
    #define WIN 12412
    #define FLAG 8233
    #define SOLVE 7173
    #define ES 5322 //empty space - not part of visible board
    
    void displayboard(int board[100][100], int x, int y);
    void createboard(int board[100][100], int x, int y, int mines);
    void solvearea(int board[100][100], int dboard[100][100], int x, int y);
    
    
    
    int main(void) {
    	//ask for board specifications
    	int x = 0, y = 0, mines = 0;
    	printf("Board length: ");
    	scanf("%d", &y);
    	printf("Board width: ");
    	scanf("%d", &x);
    	printf("Amount of mines: ");
    	scanf("%d", &mines);
    	x--; y--; mines--;
    	printf("x: %d y: %d mines: %d\n", x, y, mines);
    
    	//create the board
    	int board[100][100];
    	createboard(board, x, y, mines);
    
    	//initialize display board
    	int dboard[100][100];
    	int ix, iy;
    	for(ix = 0; ix <= 100; ix++)
    		for(iy = 0; iy <= 100; iy++)
    			dboard[ix][iy] = 0;
    
    	//main game loop
    	int flag = 0;
    	int mx, my;
    	while(flag != LOSE && flag != WIN){
    		
    	  //flag or move
    	  printf("(F)lag or (S)olve:");
    	  scanf("%d", &mx);
    	  if(mx == 'F')
    	  flag = FLAG;
    	  else if(mx == 'S'){
           	  flag = SOLVE;
    		
    	  //get move coordinates
    	  printf("\nx: ");
    	  scanf("%d", &mx);
    	  printf("\ny: ");
    	  scanf("%d", &my);
    
    	  //do the move
    	  if(flag == FLAG){
    	    dboard[mx][my] = 'F';
    	  } else if(board[mx][my] == MINE){
    	    flag = LOSE;
    	  } else{
    	    solvearea(board, dboard, mx, my);
    	  }
    
    	  displayboard(dboard, x, y);
    	  
    	}
    
    	//print results of game
    	if(flag == LOSE){
    	  printf(" ");
    	  printf("Try again next time noob");
    	} else if(flag == WIN){
    	  printf("grats bro");
    	}
    
    	return 0;
    }
    
    void displayboard(int board[100][100], int x, int y){
      int ix, iy;
      for(iy = 0; iy <= y; iy++){
        for(ix = 0; ix <= x; ix++){
          if(board[ix][iy] == 0)
    	printf("L  ");
          else if(board[ix][iy] == 'F')
    	printf("!  ");
          else if(board[ix][iy] != ES)
    	printf("%d  ", board[ix][iy]);      
        }
        printf("\n\n");
      }
    }
    
    void createboard(int board[100][100], int x, int y, int mines){
      //initialize board
      int ix, iy;
      for(iy = 0; iy <= y; iy++)
        for(ix = 0; ix <= x; ix++)
          board[ix][iy] = 0;
      
    
      //create and place mines
      srand(time(NULL));
      int i = 0, mx, my;
      while(i <= mines){
        mx = rand() % (x + 3);
        my = rand() % (y + 3);
        if(board[mx][my] != MINE){
          board[mx][my] = MINE;
          i++;
        }
      }
      
     
    
      //create number fields
      for(iy = 0; iy <= y; iy++){
        for(ix = 0; ix <= x; ix++){
          if(board[ix][iy] == MINE)
    	continue;
          else{
    	if(board[ix+1][iy] == MINE)
    	  board[ix][iy]++;
    	if(board[ix-1][iy] == MINE)
    	  board[ix][iy]++;
    	if(board[ix][iy+1] == MINE)
    	  board[ix][iy]++;
    	if(board[ix][iy-1] == MINE)
    	  board[ix][iy]++;
    	if(board[ix+1][iy+1] == MINE)
    	  board[ix][iy]++;
    	if(board[ix+1][iy-1] == MINE)
    	  board[ix][iy]++;
    	if(board[ix-1][iy+1] == MINE)
    	  board[ix][iy]++;
    	if(board[ix-1][iy-1] == MINE)
    	  board[ix][iy]++;
          }
        }
        
      }
    
    }
    
    //the algorithm in minesweeper that opens up eligible adjacent spaces when you //click on a specific space
    void solvearea(int board[100][100], int dboard[100][100], int x, int y){
      dboard[x][y] = board[x][y];
    
      if(board[x+1][y] == 0){
        dboard[x++][y] = 0;
    
        if(board[x+1][y] != 0 && board[x+1][y] != ES)
          dboard[x+1][y] = board[x+1][y];
    
        if(board[x-1][y] != 0 && board[x+1][y] != ES)
          dboard[x-1][y] = board[x-1][y];
    
        if(board[x][y+1] != 0 && board[x+1][y] != ES)
          dboard[x][y+1] = board[x][y+1];
    
        if(board[x][y-1] != 0 && board[x+1][y] != ES)
          dboard[x][y-1] = board[x][y-1];
    
        if(board[x+1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y+1] = board[x+1][y+1];
    
        if(board[x+1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y-1] = board[x+1][y-1];
    
        if(board[x-1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y+1] = board[x-1][y+1];
    
        if(board[x-1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y-1] = board[x-1][y-1];
    
        solvearea(board, dboard, x, y);
      }
    
    
      if(board[x-1][y] == 0){
        dboard[x--][y] = 0;
    
        if(board[x+1][y] != 0 && board[x+1][y] != ES)
          dboard[x+1][y] = board[x+1][y];
    
        if(board[x-1][y] != 0 && board[x+1][y] != ES)
          dboard[x-1][y] = board[x-1][y];
    
        if(board[x][y+1] != 0 && board[x+1][y] != ES)
          dboard[x][y+1] = board[x][y+1];
    
        if(board[x][y-1] != 0 && board[x+1][y] != ES)
          dboard[x][y-1] = board[x][y-1];
    
        if(board[x+1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y+1] = board[x+1][y+1];
    
        if(board[x+1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y-1] = board[x+1][y-1];
    
        if(board[x-1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y+1] = board[x-1][y+1];
    
        if(board[x-1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y-1] = board[x-1][y-1];
    
        solvearea(board, dboard, x, y);
      }
    
    
      if(board[x][y+1] == 0){
        dboard[x][y++] = 0;
    
        if(board[x+1][y] != 0 && board[x+1][y] != ES)
          dboard[x+1][y] = board[x+1][y];
    
        if(board[x-1][y] != 0 && board[x+1][y] != ES)
          dboard[x-1][y] = board[x-1][y];
    
        if(board[x][y+1] != 0 && board[x+1][y] != ES)
          dboard[x][y+1] = board[x][y+1];
    
        if(board[x][y-1] != 0 && board[x+1][y] != ES)
          dboard[x][y-1] = board[x][y-1];
    
        if(board[x+1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y+1] = board[x+1][y+1];
    
        if(board[x+1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y-1] = board[x+1][y-1];
    
        if(board[x-1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y+1] = board[x-1][y+1];
    
        if(board[x-1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y-1] = board[x-1][y-1];
    
        solvearea(board, dboard, x, y);
      }
    
    
      if(board[x][y-1] == 0){
        dboard[x][y--] = 0;
    
        if(board[x+1][y] != 0 && board[x+1][y] != ES)
          dboard[x+1][y] = board[x+1][y];
    
        if(board[x-1][y] != 0 && board[x+1][y] != ES)
          dboard[x-1][y] = board[x-1][y];
    
        if(board[x][y+1] != 0 && board[x+1][y] != ES)
          dboard[x][y+1] = board[x][y+1];
    
        if(board[x][y-1] != 0 && board[x+1][y] != ES)
          dboard[x][y-1] = board[x][y-1];
    
        if(board[x+1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y+1] = board[x+1][y+1];
    
        if(board[x+1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y-1] = board[x+1][y-1];
    
        if(board[x-1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y+1] = board[x-1][y+1];
    
        if(board[x-1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y-1] = board[x-1][y-1];
    
        solvearea(board, dboard, x, y);
      }
    
      if(board[x+1][y+1] == 0){
        dboard[x++][y++] = 0;
    
        if(board[x+1][y] != 0 && board[x+1][y] != ES)
          dboard[x+1][y] = board[x+1][y];
    
        if(board[x-1][y] != 0 && board[x+1][y] != ES)
          dboard[x-1][y] = board[x-1][y];
    
        if(board[x][y+1] != 0 && board[x+1][y] != ES)
          dboard[x][y+1] = board[x][y+1];
    
        if(board[x][y-1] != 0 && board[x+1][y] != ES)
          dboard[x][y-1] = board[x][y-1];
    
        if(board[x+1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y+1] = board[x+1][y+1];
    
        if(board[x+1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y-1] = board[x+1][y-1];
    
        if(board[x-1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y+1] = board[x-1][y+1];
    
        if(board[x-1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y-1] = board[x-1][y-1];
    
        solvearea(board, dboard, x, y);
      }
    
      if(board[x+1][y-1] == 0){
        dboard[x++][y--] = 0;
    
        if(board[x+1][y] != 0 && board[x+1][y] != ES)
          dboard[x+1][y] = board[x+1][y];
    
        if(board[x-1][y] != 0 && board[x+1][y] != ES)
          dboard[x-1][y] = board[x-1][y];
    
        if(board[x][y+1] != 0 && board[x+1][y] != ES)
          dboard[x][y+1] = board[x][y+1];
    
        if(board[x][y-1] != 0 && board[x+1][y] != ES)
          dboard[x][y-1] = board[x][y-1];
    
        if(board[x+1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y+1] = board[x+1][y+1];
    
        if(board[x+1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y-1] = board[x+1][y-1];
    
        if(board[x-1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y+1] = board[x-1][y+1];
    
        if(board[x-1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y-1] = board[x-1][y-1];
    
        solvearea(board, dboard, x, y);
      }
    
      if(board[x-1][y+1] == 0){
        dboard[x--][y++] = 0;
    
        if(board[x+1][y] != 0 && board[x+1][y] != ES)
          dboard[x+1][y] = board[x+1][y];
    
        if(board[x-1][y] != 0 && board[x+1][y] != ES)
          dboard[x-1][y] = board[x-1][y];
    
        if(board[x][y+1] != 0 && board[x+1][y] != ES)
          dboard[x][y+1] = board[x][y+1];
    
        if(board[x][y-1] != 0 && board[x+1][y] != ES)
          dboard[x][y-1] = board[x][y-1];
    
        if(board[x+1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y+1] = board[x+1][y+1];
    
        if(board[x+1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y-1] = board[x+1][y-1];
    
        if(board[x-1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y+1] = board[x-1][y+1];
    
        if(board[x-1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y-1] = board[x-1][y-1];
    
        solvearea(board, dboard, x, y);
      }
    
      if(board[x-1][y-1] == 0){
        dboard[x--][y--] = 0;
    
        if(board[x+1][y] != 0 && board[x+1][y] != ES)
          dboard[x+1][y] = board[x+1][y];
    
        if(board[x-1][y] != 0 && board[x+1][y] != ES)
          dboard[x-1][y] = board[x-1][y];
    
        if(board[x][y+1] != 0 && board[x+1][y] != ES)
          dboard[x][y+1] = board[x][y+1];
    
        if(board[x][y-1] != 0 && board[x+1][y] != ES)
          dboard[x][y-1] = board[x][y-1];
    
        if(board[x+1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y+1] = board[x+1][y+1];
    
        if(board[x+1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x+1][y-1] = board[x+1][y-1];
    
        if(board[x-1][y+1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y+1] = board[x-1][y+1];
    
        if(board[x-1][y-1] != 0 && board[x+1][y] != ES)
          dboard[x-1][y-1] = board[x-1][y-1];
    
        solvearea(board, dboard, x, y);
      }
    } //error is here -> ../src/minesweeper.c:410: error: expected declaration or statement at end of input
    Last edited by goldn; 04-28-2011 at 09:28 PM. Reason: solved

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    Code:
    else if(mx == 'S'){
    You indentation is wrong after this line; I am guessing you are missing a closing
    Code:
    }
    Tim S.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    6
    Thanks so much, I thought the problem was in the solvearea function so I kept searching that, I didn't know it could be in other parts of the program.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It may be compiler dependent but most often when I see that error on the last line of a file, it's because there is no newline at the end of the file. Going to the bottom of the file and tapping enter a couple of times usually dlears it up... all else being correct, that is/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database Revamped: 'Expected ';' before ')' token error'
    By krageon in forum C++ Programming
    Replies: 10
    Last Post: 07-08-2006, 04:03 AM
  2. Expected error
    By ZakkWylde969 in forum C Programming
    Replies: 9
    Last Post: 06-17-2003, 10:51 AM
  3. strange declaration error
    By ammar in forum C Programming
    Replies: 7
    Last Post: 12-31-2002, 03:09 AM
  4. Declaration Syntax Error
    By Im_no_Bjarne in forum C++ Programming
    Replies: 5
    Last Post: 01-25-2002, 05:38 AM
  5. Type name expected error
    By SilasP in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2001, 01:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21