title: Tic Tac Toe using C++ and multidimensional arrays
body: <p>Thanks everyone for your help, I'm new to programming and I'm not very skillful, so I appreciate your patience. I got it to work! There were several problems in the code, from the for loops used to check for "game over" and the counter used to set up the board. But the final code seems to be bug free. It prints out in the console a grid with labeled squares that allows two people to play together. Very happy right now.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  bool over = false;
  
  void print_board(char board[3][3])
  {
      cout&lt;&lt;"\n------------------\n";
      cout&lt;&lt;"\n  "&lt;&lt;board[0][0]&lt;&lt;"  |  "&lt;&lt;board[0][1]&lt;&lt; "  |  "&lt;&lt;board[0][2];
      cout&lt;&lt;"\n------------------\n";
      cout&lt;&lt;"\n  "&lt;&lt;board[1][0]&lt;&lt;"  |  "&lt;&lt;board[1][1]&lt;&lt; "  |  "&lt;&lt;board[1][2];
      cout&lt;&lt;"\n------------------\n";
      cout&lt;&lt;"\n  "&lt;&lt;board[2][0]&lt;&lt;"  |  "&lt;&lt;board[2][1]&lt;&lt; "  |  "&lt;&lt;board[2][2];
      cout&lt;&lt;"\n------------------\n";
  }
  
  bool checkForCorrectMove (char board[3][3], int choice, bool player1Turn)
  {
      int xCor;
      int yCor;
      xCor = ((choice + 2) / 3) - 1; 
      yCor = (choice - 1) % 3;
      if (((board[xCor][yCor]) != 'X') &amp;&amp; ((board[xCor][yCor]) != 'O'))
      {
          if (player1Turn)
              board[xCor][yCor] = 'X';
          if (!player1Turn)
              board[xCor][yCor] = 'O';
          return true;
      }
      else 
          return false;
  }
  
  bool checkRow(char board[3][3])
  {
      for (int i = 0; i&lt;3; i++)
      {
          if ((board[i][0] == board[i][1]) &amp;&amp; (board[i][1] == board[i][2]))
          {
              return true;
          }
      }
      return false;
  }
  bool checkColumn (char board[3][3])
  {
      for (int i = 0; i&lt;3; i++)
      {
          if ((board[0][i] == board[1][i]) &amp;&amp; (board[1][i] == board[2][i]))
          {
              return true;
          }
      }
      return false;
  }
  bool checkForWin (char board[3][3])
  {
      bool win = false;
      bool line_win = false;
      if ((board[0][0] == board[1][1]) &amp;&amp; (board[1][1] == board[2][2]))
      {
          win = true;
          return win;
      }
      if ((board[2][0] == board[1][1]) &amp;&amp; (board[1][1] == board[0][2]))
      {
          win = true;
          return win;
      }
  
      if (checkColumn(board) || checkRow (board))
      {
          win = true;
          return win;
      }
      return win;
  }
  
  int main()
  {
      cout &lt;&lt; "Welcome to Tic Tac Toe!\n";
      int choice;
      int boardcounter = 1;
      bool isOkayToMove = true;
      bool player1Turn = true;
      char board[3][3];
      for (int i = 0; i &lt; 3; i++)
      {
          for (int j = 0; j &lt; 3; j++)
          {
              board[i][j] = boardcounter + '0';
              boardcounter++;
          }
      }
      print_board(board);
      do {
          if (player1Turn)
          {
              cout &lt;&lt;"\n\nPlayer 1, please enter a number that corresponds to an open space: ";
          }
          else
          {
              cout &lt;&lt;"\n\nPlayer 2, please enter a number that corresponds to an open space: ";
          }
          cin&gt;&gt;choice;
          isOkayToMove = checkForCorrectMove(board, choice, player1Turn);
          if (isOkayToMove)
          {
              over = checkForWin(board);
              print_board(board);
              player1Turn = !player1Turn;
          }
          else
          {
              cout &lt;&lt;"\n\nYou have attempted to move into a space that is already occupied, please try again.";
              print_board(board);
          }
      }
      while (over == false);
      cout &lt;&lt;"\n\nCongratulations!";
      player1Turn = !player1Turn;
      if (player1Turn)
          cout&lt;&lt;" Player 1 has won!";
      else
          cout&lt;&lt;" Player 2 has won!";
  }
  </code></pre>
  
  <p>I'm trying to create a tic tac toe game in C++, but I'm struggling. I know this has been asked before, but it's hard to figure out what's wrong with my specific code (I'm learning C++), by looking at others' examples. Here's the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  bool over = false;
  
  void print_board(char board[3][3])
  {
      cout&lt;&lt;"\n------------------\n";
      cout&lt;&lt;"\n  "&lt;&lt;board[0][0]&lt;&lt;"  |  "&lt;&lt;board[0][1]&lt;&lt; "  |  "&lt;&lt;board[0][2];
      cout&lt;&lt;"\n------------------\n";
      cout&lt;&lt;"\n  "&lt;&lt;board[1][0]&lt;&lt;"  |  "&lt;&lt;board[1][1]&lt;&lt; "  |  "&lt;&lt;board[1][2];
      cout&lt;&lt;"\n------------------\n";
      cout&lt;&lt;"\n  "&lt;&lt;board[2][0]&lt;&lt;"  |  "&lt;&lt;board[2][1]&lt;&lt; "  |  "&lt;&lt;board[2][2];
      cout&lt;&lt;"\n------------------\n";
  }
  
  bool checkForCorrectMove (char board[][3], int choice, bool player1Turn)
  {
      int xCor;
      int yCor;
      switch(choice)
      {
          case 1: xCor = 0, yCor = 0;
          case 2: xCor = 0, yCor = 1;
          case 3: xCor = 0, yCor = 2;
          case 4: xCor = 1, yCor = 0;
          case 5: xCor = 1, yCor = 1;
          case 6: xCor = 1, yCor = 2;
          case 7: xCor = 2, yCor = 0;
          case 8: xCor = 2, yCor = 1;
          case 9: xCor = 2, yCor = 2;
      }
      if (((board[xCor][yCor]) != 'X') &amp;&amp; ((board[xCor][yCor]) != 'O'))
      {
          if (player1Turn)
              board[xCor][yCor] = 'X';
          if (!player1Turn)
              board[xCor][yCor] = 'O';
          return true;
      }
      else 
          return false;
  }
  
  bool checkForWin (char board[][3])
  {
      bool win = false;
      if ((board[0][0] == board[1][1]) &amp;&amp; (board[1][1] == board[2][2]))
      {
          win = true;
      }
      else if ((board[2][0] == board[1][1]) &amp;&amp; (board[1][1] == board[0][2]))
      {
          win = true;
      }
      for (int i = 0; i&lt;3; i++)
      {
          for (int j = 0; i &lt; 3; j++) 
          {
              if (board[i][j] != board [i][0]);
              win = false;
          }
      }
      for (int i = 0; i&lt;3; i++)
      {
          for (int j = 0; i &lt; 3; j++) 
          {
              if (board[i][j] != board [0][j]);
              win = false;
          }
      }
      return win;
  }
  
  int main()
  {
      cout &lt;&lt; "Welcome to Tic Tac Toe!\n";
      int choice;
      int boardcounter = 1;
      bool isOkayToMove = true;
      bool player1Turn = true;
      char board[3][3];
      for (int i = 0; i &lt; 3; i++)
      {
          for (int j = 0; j &lt; 3; j++)
          {
              board[i][j] = (char) boardcounter;
              boardcounter++;
          }
      }
      print_board(board);
      do {
          if (player1Turn)
          {
              cout &lt;&lt;"\n\nPlayer 1, please enter a number that corresponds to an open space: ";
          }
          else
          {
              cout &lt;&lt;"\n\nPlayer 2, please enter a number that corresponds to an open space: ";
          }
          cin&gt;&gt;choice;
          switch(choice);
          isOkayToMove = checkForCorrectMove(board, choice, player1Turn);
          if (isOkayToMove)
          {
              print_board(board);
              over = checkForWin(board);
              player1Turn = !player1Turn;
          }
          else
          {
              cout &lt;&lt;"\n\nYou have attempted to move into a space that is already occupied, please try again.";
              print_board(board);
          }
      }
      while (over == false);
      cout &lt;&lt;"\n\nCongratulations!";
      if (player1Turn)
          cout&lt;&lt;" Player 1 has won!";
      else
          cout&lt;&lt;" Player 2 has won!";
  }
  </code></pre>