2d array error

Hi I am trying to get user input for a tic-tac-toe game and am currently in the stage of checking if the inputted value and if it is an invalid number, it keeps repeating the check for input.

Only problem is that it does not recognise the input as only needing to be a number only and will constantly repeat the string over and over again leading to impaired performance of the pc.


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
27
28
29
30
31
32
33
34
35
36
37
38
    int player = 1;
    string grid[3][3];// = {'1','2','3','4','5','6','7','8','9'};
    int r,c; //r = row, c = collumn, x = grid ref
    string x;//grid ref
    int grid_number = 1; //number of grid
    int input = 0; //user number

//start game
    while(1)
    {
        
        
        
        //draw grid
        for (r=0; r<3; r++)
        {
            for (c=0;c<3; c++)
            {
                cout<<grid[r][c]<<" ";
            }    
            cout<<endl<<endl;
        }
        
        cout<<endl<<"Player "<<player<<", please input a number to play selected square: ";
        cin >> input;
        
        
        r = floor(grid_number/3)-1;
        c = ((input-1)%2)-1;
        x = grid[r][c];
        
        while(input < 1 || input > 9 || x == "X" || x == "O")
        {
             cout<<"Grid["<<r<<"]["<<c<<"] is not a valid point, please try again: ";
             cin >> input;
             system("pause");
        }
    }



all im trying to do is get it so if the user enters "4"
the computer searches grid[1][1] and check what the value is there.
else "9" will result in a search for the value at grid[2][2].but if the user enters "s" || "0" || "10" || "dwgfcdyuwegduk" it will ask for input again. Any ideas?
Last edited on
1
2
while(grid_number < 1 || grid_number > 9 || grid[floor(grid_number/3)-1][((grid_number-1)%2)-1 == '-'] =='-')
{

Whaaat? Why are you using a boolean value to get a position in an array like that?
Last edited on
LB sorry this was a problem i mistakenly wrote ,

i have edited it to show the real problem now :)
Last edited on
Topic archived. No new replies allowed.