I have been working on a C++ program that is a tic-tac-toe game.
The problem is that after I win or lose, the program does not end.

Here's the code I have written so far (I even wrote comments to make it easier):

#include <iostream>
#include <ctime>
using namespace std; 
//Purpose: Prints to the screen the an introduction and 
// provides the rules of the game
//Input: None
//Output: None
void Intro()                                
{
	cout<<"Tic-Tac-Toe";
	cout<<endl<<endl<<"Introduction:	You will play against the computer in this game.";		//Note: Tabs have been used
	cout<<"				To choose a space, type the number that matches\n		the spot you want.";
}
//Purpose: allows the computer to make a move
//Input: B[][] a 2 dimensional array that represents the board
//Output: B[][], the board that has been updated with the computer's move 
void ComputerMove(char B[][3])     
{ int i=0, turn=0;      
      cout<<"\n IN COMPUTER MOVE \n";
  while (turn==0)     //Make sure the move is ok
    { 
      i=rand()%9+1;     //randomize a move
      if (i==1)      // make sure the first location is not taken
        {
            if ((B[0][0]!='x')&&(B[0][0]!='o'))
                  {     B[0][0]='o';  //if not taken, then move
                        turn=1;
                  }           
        }
      if  (i==2)	//	make sure the second location is not taken
	  {
		  if ((B[0][1]!='x')&&((B[0][1]!='o')))
			{		B[0][1]='o';  //if not taken, then move
					turn=1;
			}
      }
	    if  (i==3)	//	make sure the third location is not taken
	  {
		  if ((B[0][2]!='x')&&((B[0][2]!='o')))
			{		B[0][2]='o';  //if not taken, then move
					turn=1;
			}
      }
		 if (i==4)      // make sure the fourth location is not taken
        {
            if ((B[1][0]!='x')&&(B[1][0]!='o'))
                  {     B[1][0]='o';  //if not taken, then move
                        turn=1;
                  }           
        }
		 if (i==5)      // make sure the first location is not taken
        {
            if ((B[1][1]!='x')&&(B[1][1]!='o'))
                  {     B[1][1]='o';  //if not taken, then move
                        turn=1;
                  }           
        }
	     if (i==6)		//make sure the sixth location is not taken
		 {
			 if ((B[1][2]!='x')&&(B[1][2]!='o'))
			 {		B[1][2]='o';	//if not taken, then move
					turn=1;
			 }
		 }
		 if (i==7)		//make sure the seventh location is not taken
		 {
			 if ((B[2][0]!='x')&&(B[2][0]!='o'))
			 {		B[2][0]='o';	//if not taken, then move
					turn=1;
			 }
		 }
	 if (i==8)		//make sure the eighth location is not taken
		 {
			 if ((B[2][1]!='x')&&(B[2][1]!='o'))
			 {		B[2][1]='o';	//if not taken, then move
					turn=1;
			 }
		 }
	 if (i==9)		//make sure the ninth location is not taken
		 {
			 if ((B[2][2]!='x')&&(B[2][2]!='o'))
			 {		B[2][2]='o';	//if not taken, then move
					turn=1;
			 }
		 }
  }
}
//Purpose: allows the user to make a move
//Input: B[][] a 2 dimensional array that represents the board
//Output: B[][], the board that has been updated with the human's move 
void HumanMove(char B[][3]) 
{int i=0, turn=0;
cout<<"\n IN HUMAN MOVE \n";
while (turn==0)     //Make sure the move is ok
    { 
      cin>>i;		//user enters move
      if (i==1)      // make sure the first location is not taken
        {
            if ((B[0][0]!='x')&&(B[0][0]!='o'))
                  {     B[0][0]='x';  //if not taken, then move
                        turn=1;
                  }           
        }
	  if  (i==2)	//	make sure the second location is not taken
	  {
		  if ((B[0][1]!='x')&&((B[0][1]!='o')))
			{		B[0][1]='x';  //if not taken, then move
					turn=1;
			}
      }
	    if  (i==3)	//	make sure the third location is not taken
	  {
		  if ((B[0][2]!='x')&&((B[0][2]!='o')))
			{		B[0][2]='x';  //if not taken, then move
					turn=1;
			}
      }
		 if (i==4)      // make sure the fourth location is not taken
        {
            if ((B[1][0]!='x')&&(B[1][0]!='o'))
                  {     B[1][0]='x';  //if not taken, then move
                        turn=1;
                  }           
        }
		 if (i==5)      // make sure the first location is not taken
        {
            if ((B[1][1]!='x')&&(B[1][1]!='o'))
                  {     B[1][1]='x';  //if not taken, then move
                        turn=1;
                  }           
        }
	     if (i==6)		//make sure the sixth location is not taken
		 {
			 if ((B[1][2]!='x')&&(B[1][2]!='o'))
			 {		B[1][2]='x';	//if not taken, then move
					turn=1;
			 }
		 }
		 if (i==7)		//make sure the seventh location is not taken
		 {
			 if ((B[2][0]!='x')&&(B[2][0]!='o'))
			 {		B[2][0]='x';	//if not taken, then move
					turn=1;
			 }
		 }
	 if (i==8)		//make sure the eighth location is not taken
		 {
			 if ((B[2][1]!='x')&&(B[2][1]!='o'))
			 {		B[2][1]='x';	//if not taken, then move
					turn=1;
			 }
		 }
	 if (i==9)		//make sure the ninth location is not taken
		 {
			 if ((B[2][2]!='x')&&(B[2][2]!='o'))
			 {		B[2][2]='x';	//if not taken, then move
					turn=1;
			 }
		 }

	}
}
//Purpose: Print out the board
//Input: B[][] a 2 dimensional array that represents the board
//Output: None
void PrintBoard(char B[][3]) 
{     cout<<"\n   "<<B[0][0]<<"|   "<<B[0][1]<<"|   "<<B[0][2];   
      cout<<"\n_________________";
      cout<<"\n   "<<B[1][0]<<"|   "<<B[1][1]<<"|   "<<B[1][2];
      cout<<"\n_________________";
      cout<<"\n   "<<B[2][0]<<"|   "<<B[2][1]<<"|   "<<B[2][2];
      cout<<"\n\n";
}
//Purpose: Check for a winner
//Input: B[][] a 2 dimensional array that represents the board
//Output: win and integer indicating winner status. 
//       0=no winner, 1= human winner, 2= computer winner
int CheckWin(char B[][3])
{     int win = 0;                                    
      cout<<"\n IN CHECK WIN "<<win<<endl;
	 if ((B[0][0]=='x')&&(B[0][1]=='x')&&(B[0][2]=='x'))		//Horizontal wins for 'x'.
		  {	win=1;
		  }
	 if ((B[1][0]=='x')&&(B[1][1]=='x')&&(B[1][2]=='x'))
		  {	win=1;
		  }
	 if ((B[2][0]=='x')&&(B[2][1]=='x')&&(B[2][2]=='x'))
		  {	win=1;
		  }
 	 if ((B[0][0]=='x')&&(B[1][0]=='x')&&(B[2][0]=='x'))		//Vertical wins for 'x'.
		  {	win=1;
		  }
  	 if ((B[0][1]=='x')&&(B[1][1]=='x')&&(B[2][1]=='x'))
		  {	win=1;
		  }
  	 if ((B[0][2]=='x')&&(B[1][2]=='x')&&(B[2][2]=='x'))
		  {	win=1;
		  }
  	 if ((B[0][0]=='x')&&(B[1][1]=='x')&&(B[2][2]=='x'))		//Top-Left diagonal win for 'x'.
		  {	win=1;
		  }
  	 if ((B[0][2]=='x')&&(B[1][1]=='x')&&(B[2][0]=='x'))		//Top-Right diagonal win for 'x'.
		  {	win=1;
		  }
	 if ((B[0][0]=='o')&&(B[0][1]=='o')&&(B[0][2]=='o'))		//Horizontal wins for 'o'.
		  {	win=2;
		  }
	 if ((B[1][0]=='o')&&(B[1][1]=='o')&&(B[1][2]=='o'))
		  {	win=2;
		  }
	 if ((B[2][0]=='o')&&(B[2][1]=='o')&&(B[2][2]=='o'))
		  {	win=2;
		  }
 	 if ((B[0][0]=='o')&&(B[1][0]=='o')&&(B[2][0]=='o'))		//Vertical wins for 'o'.
		  {	win=2;
		  }
  	 if ((B[0][1]=='o')&&(B[1][1]=='o')&&(B[2][1]=='o'))
		  {	win=2;
		  }
  	 if ((B[0][2]=='o')&&(B[1][2]=='o')&&(B[2][2]=='o'))
		  {	win=2;
		  }
  	 if ((B[0][0]=='o')&&(B[1][1]=='o')&&(B[2][2]=='o'))		//Top-Left diagonal win for 'o'.
		  {	win=2;
		  }
  	 if ((B[0][2]=='o')&&(B[1][1]=='o')&&(B[2][0]=='o'))		//Top-Right diagonal win for 'o'.
		  {	win=2;
		  }
      return (win);
}
void main ()
{int choice=0, done=0, t=0, w=0;                                              
char Board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
srand(time(0));
Intro();
PrintBoard(Board);
cout<<"Do you want to go first? Enter 1 for yes, 2 for no\n";
cin>>choice;
      if (choice==1)                                  //if player wishes to go first
      { 
            do{ cout<<"\n t = "<<t<<endl;
            HumanMove(Board);
            w=CheckWin(Board);
            if (w==1)                           
               cout<<"You win!";                                   
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            ComputerMove(Board);
            w=CheckWin(Board);
            if (w==1)
                  cout<<"You Win!";
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            t=t+1;
			}while((t!=9)||((w!=1)||(w!=2)));                                     
      }
      else                    
      {
            do{   cout<<"\n t = "<<t<<endl;
            ComputerMove(Board);
            w=CheckWin(Board);
           if(w==1)                                  cout<<"You Win!";
           else if (w==2)
                  cout<<"You lose!";
           else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            HumanMove(Board);
            w=CheckWin(Board);
            if (w==1)
                  cout<<"You Win!";
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            t=t+1;
      	}while((t!=9)||((w!=1)||(w!=2)));                               
      }
}

Any help will be greatly appreciated.

Thanks!

Your main should never return void, always an int. You should have return 0; at the end. Using another return statement in the midst of your

if (w==1)                           
   cout<<"You win!";                                   
else if (w==2)
   cout<<"You lose!";
else if (t==9&&w==0)
    cout<<"Tie Game";

might be a good idea. I haven't familiarized myself with your w codes, but if you had a statement

if(win or lose or tie)
      return 0;

and the program would exit immediately at that point.

I think there may be some improvements possible elsewhere but for now this will get your program out of being stuck.

Your main should never return void, always an int. You should have return 0; at the end. Using another return statement in the midst of your

if (w==1)                           
   cout<<"You win!";                                   
else if (w==2)
   cout<<"You lose!";
else if (t==9&&w==0)
    cout<<"Tie Game";

might be a good idea. I haven't familiarized myself with your w codes, but if you had a statement

if(win or lose or tie)
      return 0;

and the program would exit immediately at that point.

I think there may be some improvements possible elsewhere but for now this will get your program out of being stuck.

Or you could just break out of the loop, using the break; statement; so that it'll stay open long enough for you to see that you've won... :)

Your main should never return void, always an int. You should have return 0; at the end. Using another return statement in the midst of your

if (w==1)                           
   cout<<"You win!";                                   
else if (w==2)
   cout<<"You lose!";
else if (t==9&&w==0)
    cout<<"Tie Game";

might be a good idea. I haven't familiarized myself with your w codes, but if you had a statement

if(win or lose or tie)
      return 0;

and the program would exit immediately at that point.

I think there may be some improvements possible elsewhere but for now this will get your program out of being stuck.

Or you could just break out of the loop, using the break; statement; so that it'll stay open long enough for you to see that you've won... :)

I've tried some of your ideas, and I'm still having the same problem.
I think the main problem is in either the CheckWin() or main().

int CheckWin(char B[][3])
{     int win = 0;                                    
      cout<<"\n IN CHECK WIN "<<win<<endl;
	 if ((B[0][0]=='x')&&(B[0][1]=='x')&&(B[0][2]=='x'))		  {win=1;
	          }
	 if ((B[1][0]=='x')&&(B[1][1]=='x')&&(B[1][2]=='x'))
		  {	win=1;
		  }
	 if ((B[2][0]=='x')&&(B[2][1]=='x')&&(B[2][2]=='x'))
		  { win=1;
		      }
 	 if ((B[0][0]=='x')&&(B[1][0]=='x')&&(B[2][0]=='x'))		//Vertical wins for 'x'.
		  {  win=1;
		  }
  	 if ((B[0][1]=='x')&&(B[1][1]=='x')&&(B[2][1]=='x'))
		  {  win=1;
		  }
  	 if ((B[0][2]=='x')&&(B[1][2]=='x')&&(B[2][2]=='x'))
		  {  win=1;
		  }
  	 if ((B[0][0]=='x')&&(B[1][1]=='x')&&(B[2][2]=='x'))		//Top-Left diagonal win for 'x'.
		  {  win=1;
		  }
  	 if ((B[0][2]=='x')&&(B[1][1]=='x')&&(B[2][0]=='x'))		//Top-Right diagonal win for 'x'.
		  {  win=1;
		  }
	 if ((B[0][0]=='o')&&(B[0][1]=='o')&&(B[0][2]=='o'))		//Horizontal wins for 'o'.
		  {  win=2;
		  }
	 if ((B[1][0]=='o')&&(B[1][1]=='o')&&(B[1][2]=='o'))
		  {  win=2;
		  }
	 if ((B[2][0]=='o')&&(B[2][1]=='o')&&(B[2][2]=='o'))
		  {  win=2;
		  }
 	 if ((B[0][0]=='o')&&(B[1][0]=='o')&&(B[2][0]=='o'))		//Vertical wins for 'o'.
		  {  win=2;
		  }
  	 if ((B[0][1]=='o')&&(B[1][1]=='o')&&(B[2][1]=='o'))
		  {  win=2;
		  }
  	 if ((B[0][2]=='o')&&(B[1][2]=='o')&&(B[2][2]=='o'))
		  {  win=2;
		  }
  	 if ((B[0][0]=='o')&&(B[1][1]=='o')&&(B[2][2]=='o'))		//Top-Left diagonal win for 'o'.
		  {  win=2;
		  }
  	 if ((B[0][2]=='o')&&(B[1][1]=='o')&&(B[2][0]=='o'))		//Top-Right diagonal win for 'o'.
		  {  win=2;
		  }

      return (win);
}

When I run my program, after I win, it still says CheckWin=0. Is this a problem with the function or is it a problem with main()?

The game still doesn't end after a win or lose.

int main ()
{int choice=0, done=0, t=0, w=0;                                              
char Board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
srand(time(0));
Intro();
PrintBoard(Board);
cout<<"Do you want to go first? Enter 1 for yes, 2 for no\n";
cin>>choice;
      if (choice==1)                                  //if player wishes to go first
      { 
            do{ cout<<"\n t = "<<t<<endl;
            HumanMove(Board);
            w=CheckWin(Board);
            if (w==1)                           
               cout<<"You win!";                                   
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            ComputerMove(Board);
            w=CheckWin(Board);
            if (w==1)
                  cout<<"You Win!";
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            t=t+1;
			}while((t!=9)||((w!=1)||(w!=2)));                                     
      }
      else                    
      {
            do{   cout<<"\n t = "<<t<<endl;
            ComputerMove(Board);
            w=CheckWin(Board);
           if(w==1)                                  cout<<"You Win!";
           else if (w==2)
                  cout<<"You lose!";
           else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            HumanMove(Board);
            w=CheckWin(Board);
            if (w==1)
                  cout<<"You Win!";
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
            PrintBoard(Board);
            t=t+1;
      	}while((t!=9)||((w!=1)||(w!=2)));                               
      }
	  return 0;
}

Okay, after some corrections, the game DOES END if the user wins, but now if the game is a tie or a loss the loop continues and asks for HUMAN MOVE.

I know for sure now that the problem is in the main().

I tried to use the 'done' int variable to try and end the program.

int main ()
{int choice=0, done=0, t=0, w=0;                                              
char Board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
srand(time(0));
Intro();
PrintBoard(Board);
cout<<"Do you want to go first? Enter 1 for yes, 2 for no\n";
cin>>choice;
      if (choice==1)                                  //if player wishes to go first
      { 
            do{ cout<<"\n t = "<<t<<endl;
            HumanMove(Board);
			t=t+1;
            w=CheckWin(Board);
			if (w==1){                           
               cout<<"You win!";
				done=1;
			}
			else if (w==2){
                  cout<<"You lose!";
				  done=1;
			}
			else if (t==9&&w==0){
                  cout<<"Tie Game";
				  done=1;
			}
            PrintBoard(Board);
		    ComputerMove(Board);
			t=t+1;
            w=CheckWin(Board);
			if (w==1){
                  cout<<"You Win!";
				  done=1;
			}
			else if (w==2){
                  cout<<"You lose!";
				  done=1;
			}
			else if (t==9&&w==0){
                  cout<<"Tie Game";
				  done=1;
			}
            PrintBoard(Board);
			}while ((t!=9)&&((w==0))&&(done==0));                                     
      }
      else                    
      {
            do{   cout<<"\n t = "<<t<<endl;
            ComputerMove(Board);
			t=t++;
            w=CheckWin(Board);
			if(w==1){
				  cout<<"You Win!";
					}
			else if (w==2){
                  cout<<"You lose!";
					}
			else if (t==9&&w==0){
                  cout<<"Tie Game";
					}
		    PrintBoard(Board);
            HumanMove(Board);
			t=t++;
            w=CheckWin(Board);
            if (w==1)
                  cout<<"You Win!";
            else if (w==2)
                  cout<<"You lose!";
            else if (t==9&&w==0)
                  cout<<"Tie Game";
           	PrintBoard(Board);
      	}while((t!=9&&w==0)&&((t!=9)||((w==0))));                               
      }
	  cout<<"Bye!\n";
	  return 0;
}

Does anybody have any ideas for how to fix the "You lose," and "Tie Game" problem?

Any help will be greatly appreciated.

Line 72:

while((t!=9&&w==0)&&((t!=9)||((w==0))));

Even if it is correct, it definitely looks overly complex. Maybe this?

while (t < 9 && w == 0);

w == 0 means no winner yet, right? t is the number of moves so far, right? It has to be less than 9 in order for a move to be possible, right? If that's a correct read, both have to be true or the game is over.

I may be off base about this but since you are not allocating anything dynamically, could you not have

if (w==1)
{ 
         cout<<"You Win!";
         return 0;
 }
else if (w==2)
{                  
         cout<<"You lose!";
         return 0;
}
else if (t==9&&w==0)
{
         cout<<"Tie Game";
         return 0;
}

each time you have this particular control structure? (or break; instead of return 0; as amrith92 said). That way any time there's a decision you're out of the game... maybe I'm missing something. I'm also open to the fact that this is not in the spirit of "best practices."

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.