Does any can help me with my program?

Why my while loop did not work?
this is a tic tac toe game, and it is my final exam in my school.

#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;

void board(char square[9])
{
cout<<" Tic Tac Toe ";
cout<<" PLAYER 1(X) , PLAYER 2(o)\n\n";
cout<<square[0]<<"|"<<square[1]<<"|"<<square[2]<<endl;
cout<<"-------"<<endl;
cout<<square[3]<<"|"<<square[4]<<"|"<<square[5]<<endl;
cout<<"-------"<<endl;
cout<<square[6]<<"|"<<square[7]<<"|"<<square[8]<<endl;
}

string win(char square[9])
{
string win = "you win";
string keep = " PLease, keep input the number" ;


if(square[0]==square[1]&&square[1]==square[2])
return win;

else if(square[2]==square[4]&&square[4]==square[5])
return win;

else if(square[6]==square[7]&&square[7]==square[8])
return win;

else if(square[0]==square[3]&&square[3]==square[6])
return win;

else if(square[1]==square[4]&&square[4]==square[7])
return win;

else if(square[2]==square[5]&&square[5]==square[8])
return win;

else if(square[0]==square[4]&&square[4]==square[8])
return win;

else if(square[2]==square[4]&&square[4]==square[6])
return win;

else if (square[0] != '1' && square[1] != '2' && square[2] != '3' && square[3] != '4'&&square[4] != '5'
&& square[5] != '6' && square[6] != '7' && square[7] != '8' && square[8] != '9')
return keep;

else
return keep;
}

int main() {

char square[9]={'1','2','3','4','5','6','7','8','9'}; // set the graph
int i=0,judge=0;
char mark1 = 'X', mark2 = 'O';
int w = 0,choice1 = 0,choice2 = 0;

cout << "if you can use X/O to make a line, you win";// rule of the game

while (i==9){

i = i + 1;

board(square);// show the graph

judge++;
w = judge % 2; // depend who can enter the number

if (w==1)
{
cout<< " Pleas, player 1 enter a nunber which did not choose";
cin >> choice1;
cin.ignore(1000,10);

if (choice1 == 1 && square[0] == '1')
square[0] = mark1;
else if (choice1 == 2 && square[1] == '2')
square[1] = mark1;
else if (choice1 == 3 && square[2] == '3')
square[2] = mark1;
else if (choice1 == 4 && square[3] == '4')
square[3] = mark1;
else if (choice1 == 5 && square[4] == '5')
square[4] = mark1;
else if (choice1 == 6 && square[5] == '6')
square[5] = mark1;
else if (choice1 == 7 && square[6] == '7')
square[6] = mark1;
else if (choice1 == 8 && square[7] == '8')
square[7] = mark1;
else if (choice1 == 9 && square[8] == '9')
square[8] = mark1;
}// if endl
else
{
cout << "Pleas, player 2 enter a number which did not choose";
cin >> choice2;
cin.ignore(1000,10);

if (choice2 == 1 && square[0] == '1')
square[0] = mark2;
else if (choice2 == 2 && square[1] == '2')
square[1] = mark2;
else if (choice2 == 3 && square[2] == '3')
square[2] = mark2;
else if (choice2 == 4 && square[3] == '4')
square[3] = mark2;
else if (choice2 == 5 && square[4] == '5')
square[4] = mark2;
else if (choice2 == 6 && square[5] == '6')
square[5] = mark2;
else if (choice2 == 7 && square[6] == '7')
square[6] = mark2;
else if (choice2 == 8 && square[7] == '8')
square[7] = mark2;
else if (choice2 == 9 && square[8] == '9')
square[8] = mark2;
}// end of if else


win(square);
board(square);


if(win(square) == "you win" || w == 1)
{
cout << " player1 win"<< endl;
} // eng if play win2
else if(win(square) == "you win" || w == 0)
{
cout << "player2 win"<< endl;
}// end if play2 win

}

}//mian
Last edited on
Your while loop only runs when i is 9, this will never happen because you i increment is within the while loop
thanks
Topic archived. No new replies allowed.