Can not declare an int as a number

Hello.

I am trying to make a Tic Tac Toe program and when I have to make something to check if the block is taken or not, I'm going to use an int. When the block is '0', it means it is not taken and can continue putting X or O there. When it is '1', it is taken and nothing can be put there thereafter. But the problem is, when I try to declare each block's variable as '0' to start off, it gives me the following errors.

error: ISO C++ forbids itialization of member 'eightHasBeenTaken'
error: making 'eightHasBeenTaken' static
error: ISO C++ foribds in-class initalization of non-const static member 'eightHasBeenTaken'


My code line to declare those is -

int zeroHasBeenTaken, oneHasBeenTaken, twoHasBeenTaken, threeHasBeenTaken, fourHasBeenTaken, fiveHasBeenTaken, sixHasBeenTaken, sevenHasBeenTaken, eightHasBeenTaken = 0;

Thanks!
You cant initialize membervariables of a class inside the class definition. You will have do that in the constructor or create an other function (wich could be handy for re-starting the game).
Just a suggestion: I would use bool instead of integer, since it can only hold 0 (false) or 1 (true).
Hey, I started with boolean too. It's just that it had the same problem, so i tried integer.

P.S. Thanks, i'll try it.

EDIT : Another problem - when I start it, it keeps saying that my number is invalid and nothing happens, even though I type in a number 1 to 9. My code for that section is -
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
void knotsChoice()
    {
    cout << "\nEnter place number (0, 1, 2, 3, 4, 5, 6, 7 or 8)" << endl;
    cin >> choice;
    if (choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7 || choice != 8)
    {
        cout << "Invalid Number. Try again." << endl;
        system("CLS");
        drawBoard();
        knotsChoice();
    }
    else
    {
        if (choice == 0 && zeroHasBeenTaken == false)
        {
            board[0] = 'O';
            zeroHasBeenTaken = true;
        }
        else if (choice == 1 && oneHasBeenTaken == false)
        {
            board[1] = 'O';
            oneHasBeenTaken = true;
        }
        else if (choice == 2 && twoHasBeenTaken == false)
        {
            board[2] = 'O';
            twoHasBeenTaken = true;
        }
        else if (choice == 3 && threeHasBeenTaken == false)
        {
            board[3] = 'O';
            threeHasBeenTaken = true;
        }
        else if (choice == 4 && fourHasBeenTaken == false)
        {
            board[4] = 'O';
            fourHasBeenTaken = true;
        }
        else if (choice == 5 && fiveHasBeenTaken == false)
        {
            board[5] = 'O';
            fiveHasBeenTaken = true;
        }
        else if (choice == 6 && sixHasBeenTaken == false)
        {
            board[6] = 'O';
            sixHasBeenTaken = true;
        }
        else if (choice == 7 && sevenHasBeenTaken == false)
        {
            board[7] = 'O';
            sevenHasBeenTaken = true;
        }
        else if (choice == 8 && sevenHasBeenTaken == false)
        {
            board[8] = 'O';
            eightHasBeenTaken = true;
        }
        else
        {
            cout << "You must have entered a choice that has already been taken. Please try again." << endl;
            system("CLS");
            drawBoard();
            knotsChoice();
        }
        }
    }


Thanks for any help!
Last edited on
I understand your first problem is solved?

if (choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7 || choice != 8)

If choice is not equal to 1 or choice is not equal to 2 or etc. etc. then the number is invalid.

true || false = true

There can only be one of the conditions false, since choice can only be one number at a time. So, because of the above, the if-statement will always be executed.
You could use && instead, but (if choice is an integer) I would use if( !(choice>=1 && choice<=8)).
Also, I wouldnt use a construction where the function calls itself. I suggest to place the whole thing in a while-loop instead.

If choice is indeed an integer, keep in mind that using cin>>integer is asking for problems. Read this:
http://www.cplusplus.com/forum/articles/6046/

And I tought you where using classes? That may save you a lot of work here.

Hope this helps.
Last edited on
Sorry, but i've got another stupid problem. This time, it's the function for checking if there is a win or not.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
void checkWin()
    {
        if (board[0] & board[1] & board[2] == 'O')
        {
            cout << "Congratulations! KNOTS has won!" << endl;
        }
        if (board[0] & board [1] & board[2] == 'X')
        {
            cout << "Congratulations! CROSSES has won!" << endl;
        }
        if (board[3] & board[4] & board[5] == 'O')
        {
            cout << "Congratulations! KNOTS has won!" << endl;
        }
        if (board[3] & board[4] & board[5] == 'X')
        {
            cout << "Congratulations! CROSSES has won!" << endl;
        }
        if (board[6] & board[7] & board[8] == 'O')
        {
            cout << "Congratulations! KNOTS has won!" << endl;
        }
        if (board[6] & board[7] & board[8] == 'X')
        {
            cout << "Congratulations! CROSSES has won!" << endl;
        }
        if (board[0] & board[3] & board[6] == 'O')
        {
            cout << "Congratulations! KNOTS has won!" << endl;
        }
        if (board[0] & board[3] & board[6] == 'X')
        {
            cout << "Congratulations! CROSSES has won!" << endl;
        }
        if (board[1] & board[4] & board[7] == 'O')
        {
            cout << "Congratulations! KNOTS has won!" << endl;
        }
        if (board[1] & board[4] & board[7] == 'X')
        {
            cout << "Congratulations! CROSSES has won!" << endl;
        }
        if (board[2] & board[5] & board[8] == 'O')
        {
            cout << "Congratualtions! KNOTS has won!" << endl;
        }
        if (board[2] & board[5] & board[8] == 'X')
        {
            cout << "Congratulations! CROSSES has won!" << endl;
        }
        if (board[0] & board[4] & board[8] == 'O')
        {
            cout << "Congratulations! KNOTS has won!" << endl;
        }
        if (board[0] & board[4] & board[8] == 'X')
        {
            cout << "Congratulations! CROSSES has won!" << endl;
        }
        if (board[2] & board[4] & board[6] == 'O')
        {
            cout << "Congratulations! KNOTS has won!" << endl;
        }
        if (board[2] & board[4] & board[6] == 'X')
        {
            cout << "Congratulations! CROSSES has won!" << endl;
        }
    }


The problem is that sometimes if thinks it's a win even when it's not.

Thanks for any help!
Last edited on
Allow me to translate a random condition for you:

board[0] & board[4] & board[8] == 'O'
(board[0] is usually not '\0' [it would depend on your implementation]. I'll assume the board is full or that you use ' ' as empty.)
1 & board[4] & board[8] == 'O'
(board[4] is, like board[0], not '\0')
1 & 1 & board[8] == 'O'
(1&x==x. This is a fact of boolean algebra.)
1 & 1 & board[8] == 'O'
board[8] == 'O'
Last edited on
What exactly is \0?
(char)0, aka nul.
Oh.. thanks.
I'm wondering though, how would I do it if it's like that?
Just compare each thing to what you need. When you're writing, don't think it like "is A and B and C equal to D?". Think it like "is A equal to D and A equal to D and C equal to D?"

By the way, your check routine could be little more procedural.
So would I do if (board[0] == 'X' && board[1] == 'X' && board[2] == 'X'?

Thanks for the help and suggestions. How could I make my method a little more procedural?
I should use for-loops. If the boards are like this:

0 1 2
3 4 5
6 7 8


You could use someting like this (pseudo-code):
1
2
3
4
5
6
7
8
9
10
11
12
13
//first check for 012-345-678
for (int i=0;i<=6;i+=3){
   bool allX=true;
   bool allO=true;
   for (int j=0;j<=2;j++){
      if (board[i+j]!='X') allX=false;
      if (board[i+j]!='O') allO=false;
      }
   if (allX) //player 1 wins
   if (allO) //player 2 wins
   }
//the same for 036-147-258, first loop upgrade with one, second with three
//you couls also use loops for 048 and 642, but that wont make it a lot shorter 


I think it's easier to use a twodimensional array for the boards.
Last edited on
Oh jeez, that is pretty complicated, lol. Thanks for your help, I'll try again lol.
Topic archived. No new replies allowed.