WTF does my function not work?

Heres the funtion::
int y =(int) ((spot-1) / 3);
now the function seems to work fine it's when i try to draw it that it fails

heres an I/O table
1
2
3
4
5
6
7
8
9
10
11
spot| y
______|____
1   | 0
2   | 0
3   | 0
4   | 1
5   | 1
6   | 1
7   | 2
8   | 2
9   | 2


which is exactly as it should be...

so you would think that when i output
BOARD[y][x] = 'X'; (x is not the problem)
lets say i enter 1,2,or 3
it works fine.
but when i enter 4 or higher it seems to make y = 2 and then store it in the wrong spot.

7 and up doesn't evan appear but yet i even confirmed with a
cout << y;
after i store it that the I/O table is correct.

so why does it store them in the wrong spot?

(yes i could do this differently but this should work)

sorry if that seems a little clustered and unclear

Help?
can you supply more code? It would make it easier to help.
Function giving me crap...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void P1TURN()
{
    int spot;
    cout << "Player 1:  ";
    redo:
    cin >> spot;
    if (spot == 0)
    {
        GAMEON = false;
    }
    else if (spot <=9)
    {
        int x =(int) ((spot-1 % 3));
        int y =(int) ((spot-1) / 3);
        if ((BOARD[y][x] != 'X') && (BOARD[y][x] != 'O'))
        {
            BOARD[y][x] = 'X';
            cout << y;
        }
        else
        {
            cout << "\rInvalid Move! Try Again. :  ";
            goto redo;
        }
    }
}



Draw funtion (don't think this is the problem) (don't yell at me for using system... quickest and easiest solution for me.)
1
2
3
4
5
6
7
8
9
10
void DRAW_BOARD()
{
    system ("cls");
    cout << " "<<((BOARD[0][0]) ? BOARD[0][0]:' ')<<" | "<<((BOARD[0][1]) ? BOARD[0][1]:' ')<<" | "<< ((BOARD[0][2]) ? BOARD[0][2]:' ') << "     | 1, 2, 3" <<endl;
    cout << "___|___|___    |"<< endl;
    cout << " "<<((BOARD[1][0]) ? BOARD[1][0]:' ')<<" | "<<((BOARD[1][1]) ? BOARD[1][1]:' ')<<" | "<< ((BOARD[1][2]) ? BOARD[1][2]:' ') << "     | 4, 5, 6" <<endl;
    cout << "___|___|___    |"<< endl;
    cout << " "<<((BOARD[2][0]) ? BOARD[2][0]:' ')<<" | "<<((BOARD[2][1]) ? BOARD[2][1]:' ')<<" | "<< ((BOARD[2][2]) ? BOARD[2][2]:' ') << "     | 7, 8, 9" <<endl;
    cout << "   |   |       | 0 to quit"<< endl;
}


Main Func()

1
2
3
4
5
    while (GAMEON)
    {
        DRAW_BOARD();
        P1TURN();
    }


well that just a few lines short of the whole thing soo far...
(x is not the problem)


orly?

int x =(int) ((spot-1 % 3));

remember that % has a higher precedence, so:

1 % 3 = 1. You're effectively doing x = spot-1;
ZOMG THANK YOU!!! (yes caps was neccasary (along with a spell check))

i didn't think it was the problem cause it worked fine along the x axis so how did that affect my y?

nvm i dont care

thanks disch
It has to do with how 2D arrays are actually stored linearly in memory, so because you overflowed on the X axis, it would end up knocking you further forward on the Y axis.

Anyway... I could explain it if you were interested, but apparently you don't care. XD

Glad I could help.
Topic archived. No new replies allowed.