Program with Class Cards

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MonkeyHater
    New Member
    • Nov 2008
    • 13

    Program with Class Cards

    Okay, so I am have a bit of trouble with my program. The program itself will deal two hands of cards, shuffle the deck, and then deal another two hands. At the moment I have most of the code down I believe, but I cannot for the life of me figure out how to declare the numbers that I want in the array of card[]. That and I cannot figure out how to do the constructor for the class, aka theDeck. When I complied it, it came up with the error "ISO C++ forbids declaration of `theDeck' with no type". Sorry about the code being so long, but if anyone could help with figuring out what is wrong with my constructor and how to declare the numbers in the array I would be very grateful.

    Code:
    class Cards
    {
       private:
          int card[51];
       public:
          theDeck(int c);
          void Deal ();
          void Shuffle ();
    }
    Cards::theDeck(int c[])
    {
       card[51]=c[51];
    }
    void Cards::Shuffle()
    {
       srand(time(NULL));
       for (int i=0; i<(52-1); i++)
       {
          int r = i + (rand() % (52-i));
          int temp = card[i]; card[i] = card[r]; card[r] = temp;
          }
       return;
    }
    void Cards::Deal()
    {
       int r, a;
       srand(time(NULL));
       r=(rand()%51);
       a=card[r];
       if (a>100 && a<114)
       {
          if (a==101)
             cout <<"Ace of Clubs" <<endl;
          else if (a==102)
             cout <<"Two of Clubs" <<endl;
          else if (a==103)
             cout <<"Three of Clubs" <<endl;
          else if (a==104)
             cout <<"Four of Clubs" <<endl;
          else if (a==105)
             cout <<"Five of Clubs" <<endl;
          else if (a==106)
             cout <<"Six of Clubs" <<endl;
          else if (a==107)
             cout <<"Seven of Clubs" <<endl;
          else if (a==108)
             cout <<"Eight of Clubs" <<endl;
          else if (a==109)
             cout <<"Nine of Clubs" <<endl;
          else if (a==110)
             cout <<"Ten of Clubs" <<endl;
          else if (a==111)
             cout <<"Jack of Clubs" <<endl;
          else if (a==112)
             cout <<"Queen of Clubs" <<endl;
          else
             cout <<"King of Clubs" <<endl;
          }
       if (a>200 && a<214)
       {
          if (a==201)
             cout <<"Ace of Diamonds" <<endl;
          else if (a==202)
             cout <<"Two of Diamonds" <<endl;
          else if (a==203)
             cout <<"Three of Diamonds" <<endl;
          else if (a==204)
             cout <<"Four of Diamonds" <<endl;
          else if (a==205)
             cout <<"Five of Diamonds" <<endl;
          else if (a==206)
             cout <<"Six of Diamonds" <<endl;
          else if (a==207)
             cout <<"Seven of Diamonds" <<endl;
          else if (a==208)
             cout <<"Eight of Diamonds" <<endl;
          else if (a==209)
             cout <<"Nine of Diamonds" <<endl;
          else if (a==210)
             cout <<"Ten of Diamonds" <<endl;
          else if (a==211)
             cout <<"Jack of Diamonds" <<endl;
          else if (a==212)
             cout <<"Queen of Diamonds" <<endl;
          else
             cout <<"King of Diamonds" <<endl;
       }
       if (a>300 && a<314)
       {
          if (a==301)
             cout <<"Ace of Hearts" <<endl;
          else if (a==302)
             cout <<"Two of Hearts" <<endl;
          else if (a==303)
             cout <<"Three of Hearts" <<endl;
          else if (a==304)
             cout <<"Four of Hearts" <<endl;
          else if (a==305)
             cout <<"Five of Hearts" <<endl;
          else if (a==306)
             cout <<"Six of Hearts" <<endl;
          else if (a==307)
             cout <<"Seven of Hearts" <<endl;
          else if (a==308)
             cout <<"Eight of Hearts" <<endl;
          else if (a==309)
             cout <<"Nine of Hearts" <<endl;
          else if (a==310)
             cout <<"Ten of Hearts" <<endl;
          else if (a==311)
             cout <<"Jack of Hearts" <<endl;
          else if (a==312)
             cout <<"Queen of Hearts" <<endl;
          else
             cout <<"King of Hearts" <<endl;
       }
       if (a>400 && a<414)
       {
          if (a==401)
             cout <<"Ace of Spades" <<endl;
          else if (a==402)
             cout <<"Two of Spades" <<endl;
          else if (a==403)
             cout <<"Three of Spades" <<endl;
          else if (a==404)
             cout <<"Four of Spades" <<endl;
          else if (a==405)
             cout <<"Five of Spades" <<endl;
          else if (a==406)
             cout <<"Six of Spades" <<endl;
          else if (a==407)
             cout <<"Seven of Spades" <<endl;
          else if (a==408)
             cout <<"Eight of Spades" <<endl;
          else if (a==409)
             cout <<"Nine of Spades" <<endl;
          else if (a==410)
             cout <<"Ten of Spades" <<endl;
          else if (a==411)
             cout <<"Jack of Spades" <<endl;
          else if (a==412)
             cout <<"Queen of Spades" <<endl;
          else
             cout <<"King of Spades" <<endl;
       }
       return;
    }
    int main()
    {
        Cards theDeck;
        int i, j=1;
        while (j<=2)
        {
           cout <<"Hand " <<j <<endl;
           if (i=1, i<=5, i++)
           {
              theDeck.Deal();
              }
        j++;
        }
        theDeck.Shuffle();
        j=1;
        while (j<=2)
        {
           cout <<"Hand " <<j <<endl;
           if (i=1, i<=5, i++)
           {
              theDeck.Deal();
              }
        j++;
        }
        system("PAUSE");
        return 0;
    }
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    The constructor has to have the same name as the class, so it would be
    Code:
    Cards::Cards(int c[])
    As for passing an array to the constructor, do that when you declare theDeck:
    Code:
    Cards theDeck(arrayOfCards);
    Hope this helps.

    Comment

    • MonkeyHater
      New Member
      • Nov 2008
      • 13

      #3
      Okay I understand the constructor now, but when you say:
      Originally posted by boxfish
      As for passing an array to the constructor, do that when you declare theDeck:
      Code:
      Cards theDeck(arrayOfCards);
      You mean down in the main program, I understand that, but not entirely sure of how to pass it to the constructor. Do you mean that
      Code:
      Cards theDeck(card[51]={101,102,103,104,105,
                              106,107,108,109,110,
                              111,112,113,201,202,203,
                              204,205,206,207,208,
                              209,210,211,212,213,
                              301,302,303,304,305,306,
                              307,308,309,310,311,
                              312,313,401,402,403,
                              404,405,406,407,408,
                              409,410,412,413})
      If not, then I'm not really sure of what you mean.

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Don't bother with card[51]=. You can try it like this:
        Code:
        Cards theDeck({101,102,103,104,105,
                       106,107,108,109,110,
                       111,112,113,201,202,203,
                       204,205,206,207,208,
                       209,210,211,212,213,
                       301,302,303,304,305,306,
                       307,308,309,310,311,
                       312,313,401,402,403,
                       404,405,406,407,408,
                       409,410,412,413});
        but I'm not sure the compiler will like you using an array literal like that. I would do it this way:
        Code:
        int cardArray[] = {101,102,103,104,105,
                           106,107,108,109,110,
                           111,112,113,201,202,203,
                           204,205,206,207,208,
                           209,210,211,212,213,
                           301,302,303,304,305,306,
                           307,308,309,310,311,
                           312,313,401,402,403,
                           404,405,406,407,408,
                           409,410,412,413};
        Cards theDeck(cardArray);
        Hope this helps.

        Comment

        • MonkeyHater
          New Member
          • Nov 2008
          • 13

          #5
          Okay the array works perfectly. I had to use the second way you said it because the compiler didn't like it the first way. I'm getting an error with the constructor. The error is:
          "new types may not be defined in a return type
          return type specification for constructor invalid"
          Any idea on how to fix it?
          The code is:
          Code:
          class Cards
          {
             private:
                int card[51];
             public:
                Cards(int c[]);
                void Deal ();
                void Shuffle ();
          }
          Cards::Cards(int c[])
          {
             card[51]=c[51];
          }

          Comment

          • boxfish
            Recognized Expert Contributor
            • Mar 2008
            • 469

            #6
            Try putting a semicolon after the closing brace of your class. Without a semicolon, the compiler seems to think that the class definition is the return type of the constructor.
            Hope this helps.

            Comment

            • MonkeyHater
              New Member
              • Nov 2008
              • 13

              #7
              Okay, so I implemented everything mentioned above. The program "runs" sort of. It is not doing what I expected and I'm assuming it is because of trying to pass the array to the constructor. When I compile and run the program it is outputting:
              Hand 1
              25
              4072544
              33
              2293672
              1
              4072568
              9
              32
              17
              32
              Hand 2
              25
              4072544
              33
              2293672
              1
              4072568
              9
              32
              17
              32
              Hand 1
              25
              2293672
              33
              4064552
              1
              4064480
              9
              4065000
              17
              2359672
              Hand 2
              25
              2293672
              33
              4064552
              1
              4064480
              9
              4065000
              17
              2359672
              Press any key to continue . . .
              The first number is the random number I am generating, which is working fine, the second number is supposed to be the number from within the array, which is obviously wrong. My current code is:
              Code:
              #include <iostream> 
              #include <iomanip>
              
              using namespace std;
              
              class Cards
              {
                 private:
                    int card[51];
                 public:
                    Cards(int c[]);
                    void setDeck (int []);
                    void Deal ();
                    void Shuffle ();
              };
              Cards::Cards(int c[])
              {
                 card[51]=c[51];
              }
              void Cards::setDeck(int c[])
              {
                 card[51]=c[51];
                 return;
              }
              void Cards::Shuffle()
              {
                 srand(time(NULL));
                 for (int i=0; i<(52-1); i++)
                 {
                    int r = i + (rand() % (52-i));
                    int temp = card[i]; card[i] = card[r]; card[r] = temp;
                    }
                 return;
              }
              void Cards::Deal()
              {
                 int r=0,i=1;
                 srand(time(NULL));
                 while(i<=5)
                 {
                 r=(rand()%51);
                 cout <<r <<endl;
                 cout <<card[r] <<endl;;
                 if (card[r]>100 || card[r]<114)
                 {
                    if (card[r]=101)
                       cout <<"Ace of Clubs" <<endl;
                    else if (card[r]=102)
                       cout <<"Two of Clubs" <<endl;
                    else if (card[r]=103)
                       cout <<"Three of Clubs" <<endl;
                    else if (card[r]=104)
                       cout <<"Four of Clubs" <<endl;
                    else if (card[r]=105)
                       cout <<"Five of Clubs" <<endl;
                    else if (card[r]=106)
                       cout <<"Six of Clubs" <<endl;
                    else if (card[r]=107)
                       cout <<"Seven of Clubs" <<endl;
                    else if (card[r]=108)
                       cout <<"Eight of Clubs" <<endl;
                    else if (card[r]=109)
                       cout <<"Nine of Clubs" <<endl;
                    else if (card[r]=110)
                       cout <<"Ten of Clubs" <<endl;
                    else if (card[r]=111)
                       cout <<"Jack of Clubs" <<endl;
                    else if (card[r]=112)
                       cout <<"Queen of Clubs" <<endl;
                    else
                       cout <<"King of Clubs" <<endl;
                    }
                 if (card[r]>200 || card[r]<214)
                 {
                    if (card[r]=201)
                       cout <<"Ace of Diamonds" <<endl;
                    else if (card[r]=202)
                       cout <<"Two of Diamonds" <<endl;
                    else if (card[r]=203)
                       cout <<"Three of Diamonds" <<endl;
                    else if (card[r]=204)
                       cout <<"Four of Diamonds" <<endl;
                    else if (card[r]=205)
                       cout <<"Five of Diamonds" <<endl;
                    else if (card[r]=206)
                       cout <<"Six of Diamonds" <<endl;
                    else if (card[r]=207)
                       cout <<"Seven of Diamonds" <<endl;
                    else if (card[r]=208)
                       cout <<"Eight of Diamonds" <<endl;
                    else if (card[r]=209)
                       cout <<"Nine of Diamonds" <<endl;
                    else if (card[r]=210)
                       cout <<"Ten of Diamonds" <<endl;
                    else if (card[r]=211)
                       cout <<"Jack of Diamonds" <<endl;
                    else if (card[r]=212)
                       cout <<"Queen of Diamonds" <<endl;
                    else
                       cout <<"King of Diamonds" <<endl;
                 }
                 if (card[r]>300 || card[r]<314)
                 {
                    if (card[r]=301)
                       cout <<"Ace of Hearts" <<endl;
                    else if (card[r]=302)
                       cout <<"Two of Hearts" <<endl;
                    else if (card[r]=303)
                       cout <<"Three of Hearts" <<endl;
                    else if (card[r]=304)
                       cout <<"Four of Hearts" <<endl;
                    else if (card[r]=305)
                       cout <<"Five of Hearts" <<endl;
                    else if (card[r]=306)
                       cout <<"Six of Hearts" <<endl;
                    else if (card[r]=307)
                       cout <<"Seven of Hearts" <<endl;
                    else if (card[r]=308)
                       cout <<"Eight of Hearts" <<endl;
                    else if (card[r]=309)
                       cout <<"Nine of Hearts" <<endl;
                    else if (card[r]=310)
                       cout <<"Ten of Hearts" <<endl;
                    else if (card[r]=311)
                       cout <<"Jack of Hearts" <<endl;
                    else if (card[r]=312)
                       cout <<"Queen of Hearts" <<endl;
                    else
                       cout <<"King of Hearts" <<endl;
                 }
                 if (card[r]>400 || card[r]<414)
                 {
                    if (card[r]=401)
                       cout <<"Ace of Spades" <<endl;
                    else if (card[r]=402)
                       cout <<"Two of Spades" <<endl;
                    else if (card[r]=403)
                       cout <<"Three of Spades" <<endl;
                    else if (card[r]=404)
                       cout <<"Four of Spades" <<endl;
                    else if (card[r]=405)
                       cout <<"Five of Spades" <<endl;
                    else if (card[r]=406)
                       cout <<"Six of Spades" <<endl;
                    else if (card[r]=407)
                       cout <<"Seven of Spades" <<endl;
                    else if (card[r]=408)
                       cout <<"Eight of Spades" <<endl;
                    else if (card[r]=409)
                       cout <<"Nine of Spades" <<endl;
                    else if (card[r]=410)
                       cout <<"Ten of Spades" <<endl;
                    else if (card[r]=411)
                       cout <<"Jack of Spades" <<endl;
                    else if (card[r]=412)
                       cout <<"Queen of Spades" <<endl;
                    else
                       cout <<"King of Spades" <<endl;
                 }
                 i++;
                 }
                 return;
              }
              int main()
              {
                  int cardArray[52] = {101,102,103,104,105,
                                       106,107,108,109,110,
                                       111,112,113,201,202,
                                       203,204,205,206,207,
                                       208,209,210,211,212,
                                       213,301,302,303,304,
                                       305,306,307,308,309,
                                       310,311,312,313,401,
                                       402,403,404,405,406,
                                       407,408,409,410,411,
                                       412,413};
                  Cards theDeck(cardArray);
                  theDeck.setDeck(cardArray);
                  int i, j=1, r;
                  while (j<=2)
                  {
                     cout <<"Hand " <<j <<endl;
                     theDeck.Deal();
                     j++;
                  }
                  theDeck.Shuffle();
                  j=1;
                  while (j<=2)
                  {
                     cout <<"Hand " <<j <<endl;
                     theDeck.Deal();
                     j++;
                  }
                  system("PAUSE");
                  return 0;
              }
              Help please!

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                This line:

                Code:
                card[51]=c[51];
                does not do what you think it does. What you want to do is copy the elements of c into card. What you are doing is copying the 52nd element of c into the 52nd element slot of card, and leaving the rest unchanged. If your random number generated happened to generate 51 (to get the 52nd card), you'd probably get 413, like you'd expect - however, the rest of your elements are garbage.

                First, card should be declared as:

                Code:
                int card[52];
                since a standard deck contains 52 cards, not 51.

                Then, in Cards::Cards() (i.e. the constructor), don't take the 'shortcut' of assigning the pointer cards to the pointer c. Instead, manually iterate through both arrays, setting the index of cards equal to the number in c.

                Comment

                Working...