Here's my problem my assignment is to create a tic tac toe game with classes included (public and private) with only a few functions in int main.

#include <iostream>
   
      #include <string>
   
      using namespace std;
   
       
       
       
       class cgame {
             
             private:
                     
      int iTurn; 
  
      int turns; 
  
      int gameWon; 
  
      int i;
      
      
      public: 
  
       
  
      bool validMove;
  
      bool gameQuit;
      
      public:
             
            void data()
            {
                 
                 
            int  iTurn (1);
            int  turns (0);
            int   gamesWon (0);
              
            bool      validmove(false);
            bool     gamequit(false);   
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
       string squares[] = {"0","1","2","3","4","5","6","7","8","9"};
   
      string squareArray[] = {"0","1","2","3","4","5","6","7","8","9"};
  
      string playAgain = "N";
  
      string strMove; 
      
            cout << "  TIC-TAC-TOE *\n";
  
      cout << "  Player 1: X Player 2: O \n";
  
      

      cout << "\n\n\n\n";
  
       
  
      do{
  
       
  
      do{
  
      cout << "\n" << squareArray[1] << "|" << squareArray[2] << "|" << squareArray[3] << endl;
  
      cout << "-+-+-" << endl;
  
      cout << squareArray[4] << "|" << squareArray[5] << "|" << squareArray[6] << endl;
  
      cout << "-+-+-" << endl;
  
      cout << squareArray[7] << "|" << squareArray[8] << "|" << squareArray[9] << endl;
  
       
  
      while(validMove == false){
  
      cout << "\nPlayer " << iTurn << " pick a square: ";
  
      cin >> strMove;
  
      strMove = strMove.substr(0,1); // only grabs first character
  
       
  
      for(i = 1;i < 10; i++){
  
      if(squares[i] == strMove){// If true number entered (strMove) is 1-9
  
      if(squareArray[i] == strMove){// If true square != X or O
  
      validMove = true;
  
      break;
  
      }
  
      }
  
      }
  
      }
  
       
  
      validMove = false;
  
      if( iTurn == 1) { squareArray[i] = "X";} else { squareArray[i] = "O"; } // chooses sybmol
  
       
  
      // checks through possible winning combinations
  
      if(squareArray[1] != "1"){ // checks wins through square 1
  
      if(squareArray[1] == squareArray[2] && squareArray[1] == squareArray[3]){ gameWon = iTurn; }
  
      if(squareArray[1] == squareArray[4] && squareArray[1] == squareArray[7]){ gameWon = iTurn; }
  
      }
  
       
  
      if(squareArray[5] != "1"){ // checks wins through square 5 (middle)
  
      if(squareArray[5] == squareArray[1] && squareArray[5] == squareArray[9]){ gameWon = iTurn; }
  
      if(squareArray[5] == squareArray[3] && squareArray[5] == squareArray[7]){ gameWon = iTurn; }
  
      if(squareArray[5] == squareArray[4] && squareArray[5] == squareArray[6]){ gameWon = iTurn; }
  
      if(squareArray[5] == squareArray[2] && squareArray[5] == squareArray[8]){ gameWon = iTurn; }
  
      }
  
       
  
      if(squareArray[9] != "1"){ // checks wins through square 9
  
      if(squareArray[9] == squareArray[6] && squareArray[9] == squareArray[3]){ gameWon = iTurn; }
  
      if(squareArray[9] == squareArray[7] && squareArray[9] == squareArray[8]){ gameWon = iTurn; }
  
      }
  
       
  
      if( iTurn == 1) { iTurn = 2; } else { iTurn = 1; } //changes turns
  
      turns++; // adds one to total turns
  
       
  
      } while(turns < 9 && gameWon == 0);
  
       
  
      cout << "\n" << squareArray[1] << "|" << squareArray[2] << "|" << squareArray[3] << endl;
  
      cout << "-+-+-" << endl;
  
      cout << squareArray[4] << "|" << squareArray[5] << "|" << squareArray[6] << endl;
  
      cout << "-+-+-" << endl;
  
      cout << squareArray[7] << "|" << squareArray[8] << "|" << squareArray[9] << endl;
  
       
  
      if(turns >= 9){ cout << " * No Moves Left - Game Over *\n"; }
  
       
  
      if(gameWon != 0){ cout<<" **********PLAYER " << gameWon << " WINS**********\n"; }
  
       
  
      cout << "\nPlay Again (Y/N): ";
  
      cin >> playAgain;
  
       
  
      if(playAgain == "Y" || playAgain == "y"){
  
      gameWon = 0;
  
      turns = 0;
  
      squares[1] = "1";
  
      squares[2] = "2";
  
      squares[3] = "3";
  
      squares[4] = "4";
 
      squares[5] = "5";
 
      squares[6] = "6";
 
      squares[7] = "7";
 
      squares[8] = "8";
 
      squares[9] = "9";
 
       
 
      squareArray[1] = "1";
 
      squareArray[2] = "2";
 
      squareArray[3] = "3";
 
      squareArray[4] = "4";

      squareArray[5] = "5";
 
      squareArray[6] = "6";
 
      squareArray[7] = "7";
 
      squareArray[8] = "8";
 
      squareArray[9] = "9";
 
      gameQuit = false;
 
       
 
      } else { gameQuit = true; }
 
      }while(gameQuit == false);
      }
      };
             
             
             
                int main () {
                    
  char cPause;                  
                    
                    
   
cgame g1;
g1.data();

 
       
 
      cout << "\n\nThanks for playing\n";
 
      cin >> cPause;

      }

when i start the program and enter then first spot to place my x or o it ends the game. but when i hit play again everything works perfectly so im guessing it has to do with the initialization but i cant figure it out help please.

Two things (maybe)

1) std::string squareArray[] = {"0","1","2","3","4","5","6","7","8","9"}; looks scary. I would use: std::string squareArray[10] = {"0","1","2","3","4","5","6","7","8","9"}; 2) It looks like you don't initialize your member variables.

Make a constructor and in it initialize iTurn, turns, gameWon, and i.

('i' doesn't seem like a good candidate to be a member variable in the first place...)

Good luck,

David

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.