I need some help

Hi!
Can someone tell me how to remove the errors form this part of my game. I’ll be very happy to see that someone tries to solve my little problem with the 2-dimestional array.

//I tried in that way
//Here is the code

int CheckCpt(float x,float y,int objects[26][26],int im){
int x1=(int)x++;
int x2=(int)x–;
if(objects[x1][y]>0){
//main.cpp(70) : error C2108: //subscript is not of integral type
im=1;
}
if(objects[x2][y]>0){ //main.cpp(70) : error C2108: //subscript is not of integral type im=2;
}
return im;
}

//And then I tried in that way
//Here is the code

int CheckCpt(float x,float y,int objects[26][26],int im){
if(objects[x++][y]>0){
//main.cpp(70) : error C2108: //subscript is not of integral type
im=1;
}
if(objects[x–][y]>0){
//main.cpp(70) : error C2108: //subscript is not of integral type
im=2;
}
return im;
}

And in the both two ways there are the same errors.PLEASE help me.

The problem is that you cannot use floats as indices.

Which element does a[0.5] refer to ???

You need to typecast your indices if you want this to work:

int CheckCpt(float x,float y,int objects[26][26],int im)
{
int ix1=x+1;
int ix2=x-1;
int iy=y;
if(objects[ix1][iy]>0)
im=1;
if(objects[ix2][iy]>0)
im=2;
return im;
}

The funny thing is that you did it for x1 and x2 but not y !

Regards.

Eric

Thank you very much,Eric.
It works but the code returns a big number for “im” like im=-87231…
What’s the matter?

This is a copy of the e-mail I sent you to answer:

The problem is that there are probably some cases
where im is not changed (i.e. you do not go to im=1
or im=2 because neither the first or the second condition
is verified). In this case, im keeps the value it has when
entering the function (i.e. it keeps the value you give when
you call the function). Chances are, you never initialized
the variable (and most of the time, unitialized variables
take the value -87231… with VC++…).

So, just initialize it to 0 (either before calling Checkpt or at
the beginning of Checkpt) and everything will work !

Regards.

Eric

THANK YOU VERY MUCH, ERIC!!!