Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

c++ card deck class help

  • 08-03-2009 1:33pm
    #1
    Registered Users, Registered Users 2 Posts: 244 ✭✭


    as part of an assignment, i need a deck of cards class. ive done most of it but im not getting the output i expected, the main is just testing it -
    2 D (2 of diamonds)
    3 D
    4 D
    ...
    K D
    A D
    2 C
    3 C
    4 C
    ...
    K S
    A S (ace of spades)


    heres what ive got
    //deck.h
    class cDeckOfCards
    {
    private:
    int deck[51];
    int cardPointer;
    public:
    cDeckOfCards();
    void shuffle();
    void drawCard();
    };
    //deck.cpp
    #include"deck.h"
    #include<iostream>
    using namespace std;

    cDeckOfCards::cDeckOfCards()
    {
    cardPointer=0;
    int i=0;
    for (i=0; i<52; i++)
    deck=i;
    }

    void cDeckOfCards::shuffle()
    {
    }

    void cDeckOfCards::drawCard()
    {
    int tempCard=deck[cardPointer];
    if ((((tempCard+13)%13)+2)<11)
    cout << (((tempCard+13)%13)+2);
    else if ((((tempCard+13)%13)+2)==11)
    cout << "J";
    else if ((((tempCard+13)%13)+2)==12)
    cout << "Q";
    else if ((((tempCard+13)%13)+2)==13)
    cout << "K";
    else
    cout << "A";

    if ((tempCard/13)==0)
    cout << " D";
    else if ((tempCard/13)==1)
    cout << " C";
    else if ((tempCard/13)==2)
    cout << " H";
    else
    cout << " S";

    cardPointer++;
    if (cardPointer=52)
    cardPointer=0;
    }
    //main.cpp
    #include "deck.h"
    #include <iostream>

    using namespace std;

    int main()
    {
    cDeckOfCards *newDeck = new cDeckOfCards;
    int i = 0;
    for (i=0; i<200; i++)
    {
    newDeck->drawCard();
    cout << endl;
    }
    system("PAUSE");
    return 0;
    }
    the output im getting is
    A S
    2 D
    2 D
    2 D...199 times


Comments

  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    any suggestions?


  • Registered Users, Registered Users 2 Posts: 2,695 ✭✭✭Darwin


    The conditional statement:

    if (cardPointer=52)

    is in fact performing an assignment, i.e. assing 52 to cardPointer, the result of which is always true. Thus, cardPointer is reset to 0 each time drawCard() is invoked. What you want instead is an equality test:

    if (cardPointer==52)


  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    Darwin wrote: »
    The conditional statement:

    if (cardPointer=52)

    is in fact performing an assignment, i.e. assing 52 to cardPointer, the result of which is always true. Thus, cardPointer is reset to 0 each time drawCard() is invoked. What you want instead is an equality test:

    if (cardPointer==52)

    thanks:o


  • Registered Users, Registered Users 2 Posts: 1,451 ✭✭✭Onikage


    You do realise that int deck[51]; will give you an array with 51 elements, not 52?


  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    also the if statements are very difficult to follow.

    store this in a variable i.e.

    int cardNum = ((tempCard+13)%13)+2;

    then do your conditional statements based on the value inside the variable.
    also i'd tend to use a switch when there's more than one else if.

    also posting up code in [.code][./code] (removing the dots.) tags will preserve indentation.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    Onikage wrote: »
    You do realise that int deck[51]; will give you an array with 51 elements, not 52?

    damn, always forget that with the 0th term thing...


  • Registered Users, Registered Users 2 Posts: 11,198 ✭✭✭✭Crash


    Also, just as a logic thing:

    int cardNum = ((tempCard+13)%13)+2;

    is the same as

    int cardNum = (tempCard%13)+2;

    as an example, say tempcard =6;

    (6+13) % 13 is 6. as is 6%13.


  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    doing the shuffle function now and it works... but i forget how to get a different set of random numbers everytime its run...
    void cDeckOfCards::shuffle()
    {
    	int randomNum;
    	for (int i=0; i<52; i++)
    	{
    		randomNum = (rand()%52);
    		swap(deck[i], deck[randomNum]);
    	}
    }
    


  • Registered Users, Registered Users 2 Posts: 3,945 ✭✭✭Anima


    That was used in another recent thread actually. You need to set the seed. A lot of people use the time because its always changing.


Advertisement