title: Allocating array of objects dynamically freeze
body: <p>This is for an assignment before you tell me not to use an array. Unfortunately I have to.
  I've troubleshot this for a good amount of time and I've finally given up and come to ask the smart people here. I know this is something to do with how I'm allocating the array, but I can't figure what's wrong. It goes through the inner loop once and then freeze on the second run. Sorry if I left anything important out. I will add any needed info.</p>
  
  <p>Variables from Card</p>
  
  <pre><code>private:
      string *cardRank;
      string *suit;
      int rankNum;
      int value;
  </code></pre>
  
  <p>Problem Function</p>
  
  <pre><code>void initArray(Card **cPtr)
  {
      int i;
      int j;
      int index=0;
  
      cPtr = new Card*[DECK]; //deck is const int 52
  
  for(i=0; i &lt; 4; ++i)
  {
      for(j=1; j &lt; 14; ++j)
      {
  
          cPtr[index] = new Card(j, j, i); //freezes here. does not make  it to the first                               
                                           //function in the constructor
          cout &lt;&lt; cPtr[index] &lt;&lt; endl;
          ++index;
  
      }
  }
  }
  </code></pre>
  
  <p>Constructor</p>
  
  <pre><code>Card::Card(int cRank, int cValue, int suitNum)
  {
      setRankNum(cRank);
      cout &lt;&lt; "rank num set\n";
      setValue(cValue);
      cout &lt;&lt; "val set\n";
      setSuit(suitNum);
      cout &lt;&lt; "suit set\n";
      setRank(cRank);
      cout &lt;&lt; "rank set\n";
  }
  </code></pre>
  
  <p>Overloaded &lt;&lt;</p>
  
  <pre><code>ostream &amp;operator &lt;&lt; (ostream &amp;strm, Card &amp;aCard)
  {
      strm &lt;&lt; aCard.getRank() &lt;&lt; " of " &lt;&lt; aCard.getSuit();
      return strm;
  }
  </code></pre>
  
  <p>whole program</p>
  
  <pre><code>#include &lt;string&gt;
  #include "Card.h"
  using namespace std;
  
  const int DECK = 52;
  
  void initArray(Card **&amp;cPtr);
  void shufflePArray(Card **pArray);
  void determineHand(Card **pArray);
  bool isFlush(Card *hand);
  bool isStraight(Card *hand);
  bool isFour(Card *hand);
  bool isThree(Card *hand);
  bool isTwo(Card *hand);
  void drawHand(Card *hand, Card **pArray);
  void displayHand(Card **hand);
  
  int main()
  {
      Card **cArray;
      initArray(cArray);
      cout &lt;&lt; "done.";
      shufflePArray(cArray);
      determineHand(cArray);
  
  
      delete [] cArray;
      return 0;
  }
  
  
  void initArray(Card **&amp;cPtr)
  {
      int i;
      int j;
      int index=0;
  
      cPtr = new Card*[DECK];
  
      for(i=0; i &lt; 4; ++i)
      {
          for(j=1; j &lt; 13; ++j)
          { cout &lt;&lt; "inner loop " &lt;&lt; index &lt;&lt; endl;
  
          cPtr[index] = new Card(j, j, i);
          cout &lt;&lt; cPtr[index] &lt;&lt; endl;
          ++index;
          }
      }
  }
  void shufflePArray(Card **pArray)
  {
      //code here
  }
  void determineHand(Card ***pArray)
  {
      Card hand[5];
      drawHand(hand, pArray);
      displayHand(pArray);
  
      if (isFlush(hand) == true)
      {
          if(isStraight(hand) == true)
              cout &lt;&lt; "Straight flush!!!" &lt;&lt; endl;
          else
              cout &lt;&lt; "You got a flush!" &lt;&lt; endl;
      }
      else if(isStraight(hand) == true)
      {
          cout &lt;&lt; "You got a straight!" &lt;&lt; endl;
      }
      else if(isFour(hand) == true)
      {
          cout &lt;&lt; "Four of a kind!!!" &lt;&lt; endl;
      }
      else if(isThree(hand)==true)
      {
          cout &lt;&lt; "Three of a kind!" &lt;&lt; endl;
      }
      else if(isTwo(hand) == true)
      {
          cout &lt;&lt; "That's a pair";
      }
  }
  bool isFlush(Card *hand)
  {
  
      int i;
      int match;
  
      for (i=0; i&lt;5;++i)
      {
          if (hand[0].getSuit() == hand[i].getSuit())
              match++;
      }
  
      if (match == 5)
         return true;
      else
          return false;
  }
  bool isStraight(Card *hand)
  {
      int match;
  
      for (int i=0; i&lt;5;++i)
      {
          if (hand[0].getValue() == hand[i].getValue() - 1)
              match++;
      }
  
      if (match == 5)
         return true;
      else
          return false;
  }
  bool isFour(Card *hand)
  {
      int match = 0;
      for(int i=0; i&lt;5; ++i)
      {
          match=0;
          for(int j=0;j&lt;5;++j)
          {
              if (hand[i] == hand[j])
                  match++;
              if (match == 4)
                  return true;
              else
                  return false;
          }
  
      }
  }
  bool isThree(Card *hand)
  {
      int match = 0;
      for(int i=0; i&lt;5; ++i)
      {
          match=0;
          for(int j=0;j&lt;5;++j)
          {
              if (hand[i] == hand[j])
                  match++;
              if (match == 3)
                  return true;
              else
                  return false;
          }
  
      }
  }
  bool isTwo(Card *hand)
  {
      int match = 0;
      for(int i=0; i&lt;5; ++i)
      {
          match=0;
          for(int j=0;j&lt;5;++j)
          {
              if (hand[i] == hand[j])
                  match++;
              if (match == 2)
                  return true;
              else
                  return false;
          }
  
      }
  }
  
  void drawHand(Card *hand, Card **pArray)
  {
      for(int i=0; i&lt;5;++i)
       cout &lt;&lt; hand[i];
      }
  }
      {
          hand[i] = *pArray[i];
      }
  }
  
  void displayHand(Card **hand)
  {
      Card temp;
      for (int i = 0; i &lt; 5; ++i)
      {
  </code></pre>
  
  <p>contents of card.h</p>
  
  <pre><code>#ifndef CARD_H
  #define CARD_H
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  
  
  class Card
  {
      private:
          string *cardRank;
          string *suit;
          int rankNum;
          int value;
      public:
          Card();
          Card(int cRank, int cValue, int suitNum);
          friend ostream &amp;operator &lt;&lt; (ostream &amp;strm, Card &amp;aCard);
          bool operator &gt; (const Card &amp;aCard);
          bool operator &lt; (const Card &amp;aCard);
          bool operator == (const Card &amp;aCard);
          void setRank(int r);
          void setSuit(int s);
          void setValue(int v);
          void setRankNum(int n);
          string getRank();
          string getSuit();
          int getRankNum();
          int getValue();
  };
  
  #endif // CARD_H
  </code></pre>
  
  <p>contents of Card.cpp</p>
  
  <pre><code>#include "Card.h"
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  Card::Card()
  {
      cardRank = NULL;
      suit = NULL;
      rankNum = 0;
      value = 0;
  }
  
  
  Card::Card(int cRank, int cValue, int suitNum)
  {
      setRankNum(cRank);
      cout &lt;&lt; "rank num set\n";
      setValue(cValue);
      cout &lt;&lt; "val set\n";
      setSuit(suitNum);
      cout &lt;&lt; "suit set\n";
      setRank(cRank);
      cout &lt;&lt; "rank set\n";
  }
  
  ostream &amp;operator &lt;&lt; (ostream &amp;strm, Card &amp;aCard)
  {
      strm &lt;&lt; aCard.getRank() &lt;&lt; " of " &lt;&lt; aCard.getSuit();
      return strm;
  }
  
  bool Card::operator &gt; (const Card &amp;aCard)
  {
      if (aCard.value &gt; value)
          return true;
      else
          return false;
  }
  
  bool Card::operator &lt; (const Card &amp;aCard)
  {
      if (aCard.value &lt; value)
          return true;
      else
          return false;
  }
  
  bool Card::operator == (const Card &amp;aCard)
  {
      if (value == aCard.value)
          return true;
      else
          return false;
  }
  
  void Card::setRank(int r)
  {
      switch(r)
      {
      case 13:
          *cardRank = "Ace";
      case 1:
          *cardRank = "Two";
      case 2:
          *cardRank = "Three";
      case 3:
          *cardRank = "Four";
      case 4:
          *cardRank = "Five";
      case 5:
          *cardRank = "Six";
      case 6:
          *cardRank = "Seven";
      case 7:
          *cardRank = "Eight";
      case 8:
          *cardRank = "Nine";
      case 9:
          *cardRank = "Ten";
      case 10:
          *cardRank = "Jack";
      case 11:
          *cardRank = "Queen";
      case 12:
          *cardRank = "King";
      }
  }
  
  void Card::setSuit(int s)
  {
      if(s==0){
          *suit = "Hearts";
          cout &lt;&lt; "suit set";}
      else if(s==1)
          *suit = "Diamonds";
      else if (s==2)
          *suit = "Clubs";
      else if (s==3)
          *suit = "Spades";
      else
          cout &lt;&lt; "Invalid suit num.";
  }
  void Card::setValue(int v)
  {
      if (v &gt; 0)
          value = v;
  }
  void Card::setRankNum(int n)
  {
      rankNum = n;
  }
  string Card::getRank()
  {
      return *cardRank;
  }
  string Card::getSuit()
  {
      return *suit;
  }
  int Card::getRankNum()
  {
      return rankNum;
  }
  int Card::getValue()
  {
      return value;
  }
  </code></pre>