title: Connect Four C++
body: <p>I am coding the game connect four for my computer science class, everything was going great then I dont know what happened but I kept getting Sementation faults whenever I tried to run it. Here is my code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cstring&gt;
  #include &lt;cctype&gt;
  
  using namespace std;
  
  struct game{
      char **board;
      char p1;
      char p2;
  };
  
  void print_board(game p, int r, int c);
  bool play(game p, bool gamewon, int r, int c, int pieces);
  bool check(int a, int b, game p, int r, int c, int pieces);
  int drop(int b, char player, game p, int c);
  
  
  int main(int argc, char *argv[]){
      game p;
      int r, c, pieces;
      bool gamewon = false;
      /*if(argc == 7) {
         for(int i = 1; i &lt; argc; i += 2) {
            if(argv[i][0]=='-' &amp;&amp; argv[i][1]=='r')
               r = atoi(argv[i+1]);
            else if(!strcmp(argv[i],"-c"))
               c = atoi(argv[i+1]);
            else if(!strcmp(argv[i],"-p")){
               pieces = atoi(argv[i+1]);
               while (pieces &lt; 1){
                  cout &lt;&lt; "You cannot have 0 pieces to connect." &lt;&lt; endl;
                  cout &lt;&lt; "Please enter a positive, non-zero integer"
                  &lt;&lt; " for the number of pieces to connect: ";
                  cin &gt;&gt; pieces;
                  }
            }
            else 
               cout &lt;&lt; "Error." &lt;&lt; endl;      
         }
     }*/
      r = 6;
      c = 7;
      pieces = 4; 
      p.board = new char*[r];
      for(int i=0; i &lt; r;i++)
          p.board[i] = new char[c]; 
      for(int a =0; a &lt; r; a++){      
          for(int b = 0; b &lt; c; b++)  
              p.board[a][b] = ' ';        
      }                               
      print_board(p, r, c);
          gamewon = play(p, gamewon, r, c, pieces);
      return 0;
  }
  
  
  
  bool play(game p, bool gamewon, int r, int c, int pieces){
      int col, hold = 0, charsPlaced = 0;
      char player = 'y';
      while(!gamewon){
          if(hold != -1){
              if(player =='y'){
                  cout&lt;&lt;"Player 1, what column do you want to put your piece? ";
                  player = 'r';
              }
              else{
                  cout&lt;&lt;"Player 2, what column do you want to put your piece? ";
                  player = 'y';
              }
          }
          while(true){
              if(charsPlaced == r*c) break;
              cin&gt;&gt;col;
              col--;
              if(col &lt;=r &amp;&amp; col&gt;= 0) break;
              else cout&lt;&lt; "\nPlease enter a value between 1 and " &lt;&lt; c &lt;&lt; ": ";
              if (cin.fail()){                    
                  cin.clear();        
                  char d;         
                  cin&gt;&gt;d;     
              }                   
  
          }
          if(charsPlaced == r*c) break;
          hold = drop(col,player, p, c);
          if(hold == -1)  cout&lt;&lt;"Column is full!\nPlease enter another number between 1 and " &lt;&lt; c &lt;&lt; ": ";
          else{
              gamewon = check(hold, col, p, r, c, pieces);
              charsPlaced ++;
              print_board(p, r, c);
          }
      }
      if(charsPlaced == r*c){
          cout&lt;&lt;"No winner! Game is a draw\n";
          return true;
      }
      if(player == 'y')
          cout&lt;&lt;"Player 2 is the winner!\n";
      else cout&lt;&lt;"Player 1 is the winner!\n";
      return true;
  
  }    
  
  void print_board(game p, int r, int c){
      cout &lt;&lt; endl;
      for(int a = 0; a &lt; r; a++){
          for(int b = 0; b &lt; c; b++) 
             cout &lt;&lt; "|" &lt;&lt; p.board[a][b];
             cout &lt;&lt; "|";
             cout &lt;&lt; endl;
             for(int i = 0; i &lt; c; i++)
                cout &lt;&lt; "--";
                cout &lt;&lt; endl;
      }
  }
  
  bool check(int a, int b, game p, int r, int c, int pieces){
      int vertical = 1, horizontal = 1, diagonalone = 1, diagonaltwo = 1, i , j;
      cout &lt;&lt; i &lt;&lt; " " &lt;&lt; b &lt;&lt; " " &lt;&lt; a &lt;&lt; endl;
      char player = p.board[a][b];
      cout &lt;&lt; player &lt;&lt; endl;
      for(i = a + 1; p.board[i][b] == player &amp;&amp; i &lt; r; i++, vertical++);
      for(i = a - 1; p.board[i][b] == player &amp;&amp; i &gt;= 0; i--, vertical++);
      if(vertical &gt;= pieces)
          return true;
      for(j = b - 1; p.board[a][j] == player &amp;&amp; j &gt;= 0; j--, horizontal++);
      for(j = b + 1; p.board[a][j] == player &amp;&amp; j &lt; c; j++, horizontal++);
      if(horizontal &gt;= pieces) 
          return true;
      for(i = a - 1, j = b - 1; p.board[i][j] == player &amp;&amp; i &gt;= 0 &amp;&amp; j &gt;= 0; diagonalone++, i--, j--);
      for(i = a + 1, j = b + 1; p.board[i][j] == player &amp;&amp; i &lt;= r &amp;&amp; j &lt;= c;diagonalone++, i++, j++);
      if(diagonalone &gt;= pieces) 
          return true;
      for(i = a - 1, j = b + 1; p.board[i][j] == player &amp;&amp; i &gt;= 0 &amp;&amp; j &lt;= c; diagonaltwo++, i--, j++);
      for(i = a + 1, j = b - 1; p.board[i][j] == player &amp;&amp; i &lt;= r &amp;&amp; j &gt;= 0; diagonaltwo++, i++, j--);
      if(diagonaltwo &gt;= pieces) 
          return true;
      return false;
  }
  
  int drop(int b, char player, game p, int c){
      if(b &gt;= 0 &amp;&amp; b &lt;= c){
          if(p.board[0][b] == ' '){
              int i;
              for(i = 0; p.board[i][b] == ' '; i++)
                  if(i == 5){
                      p.board[i][b] = player;
                      return i;
              }
              i--;
              p.board[i][b] = player;
              return i;
  
          }
          else{
              return -1;
          }
  
      }
      else{
          return -1;
      }
  
  }
  </code></pre>