开发者

C code won't compile [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

Please help me to understand why the following code will not compile:

#include <stdio.h>
//#include <iostream>
//using namespace std;
int main(void){
    int i,k,x,y,run,e,r,s,m,count=0;

char numbers[19][19];
for(i=0;i<19;i++){
          for (k=0;k<19;k++){
                   numbers[i][k]='.';
          }
}

void drawB(){
    printf("  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8 \n");

     printf  ("0 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[0][0],numbers[0][1],numbers[0][2],numbers[0][3],numbers[0][4],
     numbers[0][5],numbers[0][6],numbers[0][7],numbers[0][8],numbers[0][9],
     numbers[0][10],numbers[1][11],numbers[1][12],numbers[1][13],numbers[0][14]
     ,numbers[0][15],numbers[0][16],numbers[0][17],numbers[0][18]);

     printf  ("1 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[1][0],numbers[1][1],numbers[1][2],numbers[1][3],numbers[1][4],
     numbers[1][5],numbers[1][6],numbers[1][7],numbers[1][8],numbers[1][9],
     numbers[1][10],numbers[1][11],numbers[1][12],numbers[1][13],numbers[1][14]
     ,numbers[1][15],numbers[1][16],numbers[1][17],numbers[1][18]);

               printf ("2 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     numbers[2][0],numbers[2][1],numbers[2][2],numbers[2][3],numbers[2][4],
     numbers[2][5],numbers[2][6],numbers[2][7],numbers[2][8],numbers[2][9],
     numbers[2][10],numbers[2][11],numbers[2][12],numbers[2][13],numbers[2][14]
     ,numbers[2][15],numbers[2][16],numbers[2][17],numbers[2][18]);

       printf ("3 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[3][0],numbers[3][1],numbers[3][2],numbers[3][3],numbers[3][4],
     numbers[3][5],numbers[3][6],numbers[3][7],numbers[3][8],numbers[3][9],
     numbers[3][10],numbers[3][11],numbers[3][12],numbers[3][13],numbers[3][14]
     ,numbers[3][15],numbers[3][16],numbers[3][17],numbers[3][18]);

       printf ("4 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[4][0],numbers[4][1],numbers[4][2],numbers[4][3],numbers[4][4],
     numbers[4][5],numbers[4][6],numbers[4][7],numbers[4][8],numbers[4][9],
     numbers[4][10],numbers[4][11],numbers[4][12],numbers[4][13],numbers[4][14]
     ,numbers[4][15],numbers[4][16],numbers[4][17],numbers[4][18]);

       printf ("5 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[5][0],numbers[5][1],numbers[5][2],numbers[5][3],numbers[5][4],
     numbers[5][5],numbers[5][6],numbers[5][7],numbers[5][8],numbers[5][9],
     numbers[5][10],numbers[5][11],numbers[5][12],numbers[5][13],numbers[5][14]
     ,numbers[5][15],numbers[5][16],numbers[5][17],numbers[5][18]);

       printf ("6 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[6][0],numbers[6][1],numbers[6][2],numbers[6][3],numbers[6][4],
     numbers[6][5],numbers[6][6],numbers[6][7],numbers[6][8],numbers[6][9]开发者_如何学JAVA,
     numbers[6][10],numbers[6][11],numbers[6][12],numbers[6][13],numbers[6][14]
     ,numbers[6][15],numbers[6][16],numbers[6][17],numbers[6][18]);

       printf ("7 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[7][0],numbers[7][1],numbers[7][2],numbers[7][3],numbers[7][4],
     numbers[7][5],numbers[7][6],numbers[7][7],numbers[7][8],numbers[7][9],
     numbers[7][10],numbers[7][11],numbers[7][12],numbers[7][13],numbers[7][14]
     ,numbers[7][15],numbers[7][16],numbers[7][17],numbers[7][18]);

       printf ("8 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[8][0],numbers[8][1],numbers[8][2],numbers[8][3],numbers[8][4],
     numbers[8][5],numbers[8][6],numbers[8][7],numbers[8][8],numbers[8][9],
     numbers[8][10],numbers[8][11],numbers[8][12],numbers[8][13],numbers[8][14]
     ,numbers[8][15],numbers[8][16],numbers[8][17],numbers[8][18]);

       printf ("9 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[9][0],numbers[9][1],numbers[9][2],numbers[9][3],numbers[9][4],
     numbers[9][5],numbers[9][6],numbers[9][7],numbers[9][8],numbers[9][9],
     numbers[9][10],numbers[9][11],numbers[9][12],numbers[9][13],numbers[9][14]
     ,numbers[9][15],numbers[9][16],numbers[9][17],numbers[9][18]);

       printf ("0 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[10][0],numbers[10][1],numbers[10][2],numbers[10][3],numbers[10][4],
     numbers[10][5],numbers[10][6],numbers[10][7],numbers[10][8],numbers[10][9],
     numbers[10][10],numbers[10][11],numbers[10][12],numbers[10][13],numbers[10][14]
     ,numbers[10][15],numbers[10][16],numbers[10][17],numbers[10][18]);

       printf ("1 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[11][0],numbers[11][1],numbers[11][2],numbers[11][3],numbers[11][4],
     numbers[11][5],numbers[11][6],numbers[11][7],numbers[11][8],numbers[11][9],
     numbers[11][10],numbers[11][11],numbers[11][12],numbers[11][13],numbers[11][14]
     ,numbers[11][15],numbers[11][16],numbers[11][17],numbers[11][18]);

               printf ("2 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[12][0],numbers[12][1],numbers[12][2],numbers[12][3],numbers[12][4],
     numbers[12][5],numbers[12][6],numbers[12][7],numbers[12][8],numbers[12][9],
     numbers[12][10],numbers[12][11],numbers[12][12],numbers[12][13],numbers[12][14]
     ,numbers[12][15],numbers[12][16],numbers[12][17],numbers[12][18]);

       printf ("3 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[13][0],numbers[13][1],numbers[13][2],numbers[13][3],numbers[13][4],
     numbers[13][5],numbers[13][6],numbers[13][7],numbers[13][8],numbers[13][9],
     numbers[13][10],numbers[13][11],numbers[13][12],numbers[13][13],numbers[13][14]
     ,numbers[13][15],numbers[13][16],numbers[13][17],numbers[13][18]);

       printf ("4 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[14][0],numbers[14][1],numbers[14][2],numbers[14][3],numbers[14][4],
     numbers[14][5],numbers[14][6],numbers[14][7],numbers[14][8],numbers[14][9],
     numbers[14][10],numbers[14][11],numbers[14][12],numbers[14][13],numbers[14][14]
     ,numbers[14][15],numbers[14][16],numbers[14][17],numbers[14][18]);

       printf ("5 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[15][0],numbers[15][1],numbers[15][2],numbers[15][3],numbers[15][4],
     numbers[15][5],numbers[15][6],numbers[15][7],numbers[15][8],numbers[15][9],
     numbers[15][10],numbers[15][11],numbers[15][12],numbers[15][13],numbers[15][14]
     ,numbers[15][15],numbers[15][16],numbers[15][17],numbers[15][18]);

       printf ("6 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[16][0],numbers[16][1],numbers[16][2],numbers[16][3],numbers[16][4],
     numbers[16][5],numbers[16][6],numbers[16][7],numbers[16][8],numbers[16][9],
     numbers[16][10],numbers[16][11],numbers[16][12],numbers[16][13],numbers[16][14]
     ,numbers[16][15],numbers[16][16],numbers[16][17],numbers[16][18]);

       printf ("7 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[17][0],numbers[17][1],numbers[17][2],numbers[17][3],numbers[17][4],
     numbers[17][5],numbers[17][6],numbers[17][7],numbers[17][8],numbers[17][9],
     numbers[17][10],numbers[17][11],numbers[17][12],numbers[17][13],numbers[17][14]
     ,numbers[17][15],numbers[17][16],numbers[17][17],numbers[17][18]);

       printf ("8 %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  \n\n"
     ,numbers[18][0],numbers[18][1],numbers[18][2],numbers[18][3],numbers[18][4],
     numbers[18][5],numbers[18][6],numbers[18][7],numbers[18][8],numbers[18][9],
     numbers[18][10],numbers[18][11],numbers[18][12],numbers[18][13],numbers[18][14]
     ,numbers[18][15],numbers[18][16],numbers[18][17],numbers[18][18]);

}
void checkSurrounded (int x,int y){


//numbers [x-1,y-1 ] , numbers [x-1,y ] , numbers [x-1,y+1 ]
//numbers [x,y-1 ] , numbers [x,y ] , numbers [x,y+1 ]
//numbers [x+1,y-1, ] , numbers [x+1,y ] , numbers [x+1,y+1 ]

if(numbers[x][y]=='A'){
  if((numbers[x-1][y-1]=='B') && (numbers[x-1][y]=='B') &&
  (numbers[x-1][y+1]=='B') && (numbers[x][y-1]=='B') &&
  (numbers[x][y+1]=='B') &&  (numbers[x+1][y-1]=='B') &&
  (numbers[x+1][y]=='B')){
  numbers[x][y]='B';}

                      }
if(numbers[x][y]=='B'){
  if((numbers[x-1][y-1]=='A') && (numbers[x-1][y]=='A') &&
  (numbers[x-1][y+1]=='A') && (numbers[x][y-1]=='A') &&
  (numbers[x][y+1]=='A') &&  (numbers[x+1][y-1]=='A') &&
  (numbers[x+1][y]=='A')){
  numbers[x][y]='A';
  }
 }
 }
/**
 void checkArea(){
 //detect enemy stone
 //store in array
 //find adjacent enemy stones
 // store the enemy stones in the array
 //keep on doing this until there are no more enemy stones that are adjacent

for (s=0;s<19;s++) {
   for (m=0;m<19;m++) {
       if (numbers[s][m]=='A'){
             count++;
       }
   }
}



}//end fn

void checkAdjacent(int x, int y){
    if (numbers [x][y]=='A'){
       if((numbers[x][y-1]=='B' && numbers [x-1][y]=='B' && numbers[x][y+1]=='B' && numbers[x+1][y]=='B')){

       }
    }

}


void getUserInput(){

    int run=1;
    while(run){
               printf("Enter x coordinate\n");
               scanf("%d",&x);
               printf("Enter y coordinate\n");
               scanf("%d",&y);

               if((x>18 || y>18 || x<0 || y<0) && !( numbers[x][y]=='.' )){
                       printf("invalid imput\n");
               }

               else{
                    numbers[x][y]='B';
                    run=0;
                    drawB();


               }
    }
}
*/
void getCupInput(){
    //go through borad
    //starting from [0][0]
    //stop at first player stone
    //save as target x and target y
    //surround target x and target y
    //if already surrounded
    //start looking in borad again from last position
    //at end of board reset to [0][0]



    for(s=0;s<19;s++) {
      for(m=0;m<19;m++) {
         if (numbers[s][m]==0){
             count++;

          }
      }
  }


    x=x-2;
    y=y+2;
    numbers[x][y]='A';
    drawB();
}

 while(1){
        //getUserInput();
     getCupInput();
 }
         system("pause");

 return 0;

}


In C function def cannot be nested and that is what you are doing:

int main(void){                      // start of main fun def.
    int i,k,x,y,run,e,r,s,m,count=0;

char numbers[19][19];
for(i=0;i<19;i++){
          for (k=0;k<19;k++){
                   numbers[i][k]='.';
          }                         // end of inner for loop.
}                                   // end of outer for loop.


void drawB(){  //<--------- main() not yet completed..you've a new fun def.


This looks like the very very beginnings of an attempt at a program for Go (the board game, not the recent programming language.) Anyway, there are many issues with this code, but as far as failing to compile, I can see at least one. With much, much code deleted, you very roughly have this:

int main(void) {
    char numbers[19][19];
    /* stuff */
}
void drawB(void) {
    printf("...", ..., numbers[X][Y], ...);
}

Inside drawB, the code cannot see the declaration of numbers inside of main, so the compiler will be giving you some sort of error about an undeclared variable drawB. You need to move the declaration of numbers outside of main, so that both main, drawB, and all the other functions that reference numbers can see it.

@unicornaddict has pointed out that you've actually nested the defintion of drawB inside of main. Perhaps you meant to do this, or perhaps it's just impossible to tell because of the inconsistent indentation inside of main. Either way, once you fix the nesting problem, you'll have to move the declaration of numbers outside of your functions. So, your code should now look a bit like this:

char numbers[19][19];
int main(void) {
    /* stuff */
}
void drawB(void) {
    printf("...", ..., numbers[X][Y], ...);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜