#include<iostream>
    #include<conio.h>
    using namespace std;
    char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
    void box();
    int checkwin();
    int main()
    {
        char f, g,  e[10]={'o','1','2','3','4','5','6','7','8','9'};
        int a;
        int   c, d;
        for(a=1;(a<=9);a++)
        {
            int b;
            system("CLS");
            box();
    if(a%2==1)
    {
        cout<<"player 1 enter your num\n";
        cin>>b;
    }

    else 
    {
        cout<<"player 2 enter you number\n";
    cin>>c; 
    }
    if(b==1&&square[1]=='1')
    square[1]='X';
    else if(b==2&&square[2]=='2')
    square[2]=='X';
    else if(c==1&&square[1]=='1')
    square[1]='O';
    }
    getch();
    return 0;
    }
    void box()
    {
        system("CLS");
        cout << "\n\n\tTic Tac Toe\n\n";
        cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
        cout << endl;
        cout << "     |     |     " << endl;
        cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;
        cout << "_____|_____|_____" << endl;
        cout << "     |     |     " << endl;
        cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;
        cout << "_____|_____|_____" << endl;
        cout << "     |     |     " << endl;
        cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;
        cout << "     |     |     " << endl << endl;
    }

i was working on tic tac toe, but i am stuck in the middle..
if you compiler, when player 1 presses 2, nothing happens....but works fine when you press 1....why the conditions after the first condition, not working!!

Recommended Answers

All 4 Replies

You are not initializing your variables to known "good" values, so they contain "garbage" (whatever was in memory at that location) until you specifically set them. In this case, the integer 'b' is being tested after checking 'a' in this line: a%2 == 1 with the code if(b==1&&square[1]=='1'), yet if a%2 != 1 then it was never set, resulting in your problem.

If i initialise b and c to 1 '''' will it fix my problem!!!

I doubt it. Looks like everything is initialized fine to me. But look at the difference between the operators used in lines 29 and 31. One of them is correct, the other is wrong. Fix the mistake and I think you will get the response you want.

Your program will get very long however if you keep following the logic you are displaying here, however. Loops and arrays go together like milk and cookies, so take advantage of their power.

Are you sure you entered value 2 for player 1 and not for player 2?

Just a hint. Replace this:

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
// ...
if(a%2==1)
{
    cout<<"player 1 enter your num\n";
    cin>>b;
}
else
{
    cout<<"player 2 enter you number\n";
    cin>>c;
}
if(b==1&&square[1]=='1')
square[1]='X';
else if(b==2&&square[2]=='2')
square[2]=='X';
else if(c==1&&square[1]=='1')
square[1]='O';

with this:

char square[9] = {'1','2','3','4','5','6','7','8','9'};
// ...
cout << "player " << (2 - (a%2)) << "enter your num" << endl;
cin >> b;
b--;
// square[b] = a%2 ? 'X' : 'O' is equivalent with
// if (a%2) square[b] = 'X';
// else square[b] = 'O';
if ( square[b] == ('1'+b) ) square[b] = a%2 ? 'X' : 'O';

It will shorten your code quite some.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.