title: sudoku solving in c++ using class
body: <p>This sudoku solving program is getting compiled but getting error "segmentation fault " please help me in solving this problem and clarify why i am getting segmentation error as i have written every thing properlu .thanks in advance</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;math.h&gt;
  
  class sudoku
  {
  public:
      sudoku();
      void initializeSudokuGrid();
      void printSudokuGrid();
      bool solveSudoku();
      bool findEmptyGridSlot(int &amp;row, int &amp;col);
      bool canPlaceNum(int row, int col, int num);
      bool numAlreadyInRow(int row, int num);
      bool numAlreadyInCol(int col, int num);
      bool numAlreadyInBox(int smallGridRow, int smallGridCol, int num);
      int grid[9][9];
  };
  
  sudoku::sudoku()
  {
      for (int i = 0; i &lt; 9; i++)
      {
          for (int j = 0; j &lt; 9; j++)
          {
              grid[i][j] = 0;
          }
      }
      std::cout &lt;&lt; "\n all the grid locations are initialise to zero";
  }
  
  void sudoku::initializeSudokuGrid()
  {
      char x = 'y';
      while (x == 'y')
      {
          int row, col, var;
          std::cout
                  &lt;&lt; "\n enter the row,column and integer in the box(that is 1-9 numbers \n";
          std::cin &gt;&gt; row;
          std::cin &gt;&gt; col;
          std::cin &gt;&gt; var;
          grid[row][col] = var;
          std::cout
                  &lt;&lt; "\n are there any slots that u want to enter the numbers into the boxs enter y else enter n \n";
          std::cin &gt;&gt; x;
      }
  }
  
  void sudoku::printSudokuGrid()
  {
      std::cout &lt;&lt; "\n";
      for (int i = 0; i &lt; 9; i++)
      {
          for (int j = 0; j &lt; 9; j++)
          {
              std::cout &lt;&lt; grid[i][j] &lt;&lt; " ";
          }
          std::cout &lt;&lt; "\n";
      }
  }
  
  bool sudoku::solveSudoku()
  {
      int row, col;
      if (findEmptyGridSlot(row, col))
      {
          for (int num = 1; num &lt;= 9; num++)
          {
              if (canPlaceNum(row, col, num))
              {
                  grid[row][col] = num;
                  if (solveSudoku()) //recursive call
                      return true;
                  grid[row][col] = 0;
              }
          }
          return false; //backtrack
      }
      else
          return true; //there are no empty slots
  }
  
  bool sudoku::numAlreadyInRow(int row, int num)
  {
      for (int i = 0; i &lt; 9; i++)
      {
          if (num != 0 &amp;&amp; grid[row][i] == num)
              return true;
      }
      return false;
  }
  
  bool sudoku::numAlreadyInCol(int col, int num)
  {
      for (int i = 0; i &lt; 9; i++)
      {
          if (num != 0 &amp;&amp; grid[i][col] == num)
              return true;
      }
      return false;
  }
  
  bool sudoku::canPlaceNum(int row, int col, int num)
  {
      if (!numAlreadyInRow(row, num))
      {
          if (!numAlreadyInCol(col, num))
          {
              int smallGridRow = row - row % 3;
              int smallGridCol = col - col % 3;
              if (!numAlreadyInBox(smallGridRow, smallGridCol, num))
              {
                  return true;
              }
          }
      }
      return false;
  }
  
  bool sudoku::numAlreadyInBox(int smallGridRow, int smallGridCol, int num)
  {
      for (int i = 0; i &lt; 3; i++)
      {
          for (int j = 0; j &lt; 3; j++)
          {
              if (grid[i + smallGridRow][j + smallGridCol] == num)
                  return true;
          }
      }
      return false;
  }
  
  bool sudoku::findEmptyGridSlot(int &amp;row, int &amp;col)
  {
      for (int row = 0; row &lt; 9; row++)
      {
          for (int col = 0; col &lt; 9; col++)
          {
              if (grid[row][col] == 0)
                  return true;
          }
      }
      return false;
  }
  
  int main()
  {
      sudoku s;
      s.printSudokuGrid();
      s.initializeSudokuGrid();
      s.printSudokuGrid();
      std::cout &lt;&lt; "\n after solving the problem \n";
      if (s.solveSudoku())
          s.printSudokuGrid();
      else
          std::cout &lt;&lt; "\n solution doesnt exist for this type of solution \n";
      return 0;
  }
  </code></pre>