#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <time.h>
usingnamespace std;
class Cards
{
public:
int ncardValue; // assigned 2-10. Assigned 0 for JQKA.
string scardValue; // assigned to JQKA. Not defined for 2-10.
string scardSuit; // Diamond, Club, Heart, Spade
bool bcardDrawn; // false if still in deck.
};
Cards* buildDeck(void)
{
Cards *cardDeck = NULL;
cardDeck = new Cards[52];
for (int i=0; i<52; i++)
{
cardDeck[i].bcardDrawn = false;
cardDeck[i].ncardValue = 0;
if ( i < 9 )
cardDeck[i].ncardValue = // all of these are equal. Offsets for each suit.
cardDeck[i+13].ncardValue =
cardDeck[i+26].ncardValue =
cardDeck[i+39].ncardValue = i+2;
if (i > 0 && i <= 12)
cardDeck[i].scardSuit = "Diamond";
if (i > 12 && i <= 25)
cardDeck[i].scardSuit = "Club";
if (i > 25 && i <= 38)
cardDeck[i].scardSuit = "Heart";
if (i > 38 && i <= 51)
cardDeck[i].scardSuit = "Spade";
if ( (i+1)%10 == 0 ) // jack=9i (since 2=0i, 10 = 8i)
cardDeck[i].scardValue = "Jack";
if ( (i+1)%11 == 0 )
cardDeck[i].scardValue = "Queen";
if ( (i+1)%12 == 0 )
cardDeck[i].scardValue = "King";
if ( (i+1)%13 == 0 )
cardDeck[i].scardValue = "Ace";
}
return cardDeck;
}
Cards* drawCards(int numCards, Cards fullDeck[])
{
Cards *userHand = NULL;
userHand = new Cards[numCards];
int currCard;
for (int i=0; i<6; i++)
{
currCard = rand()%52;
if (fullDeck[currCard].bcardDrawn != true)
userHand[i] = fullDeck[currCard];
fullDeck[currCard].bcardDrawn = true;
}
return userHand;
}
int main()
{
Cards* deck = buildDeck();
srand(time(0));
int numCards;
cout << "How many cards do you want to draw?" << endl;
cin >> numCards;
Cards* hand = drawCards(numCards, deck);
/*for (int i=0; i<numCards+1; i++)
{
cout << i+1 << ": ";
if ( hand[i].ncardValue == 0 )
cout << hand[i].scardValue;
if ( hand[i].ncardValue )
cout << hand[i].ncardValue;
cout << " " << hand[i].scardSuit << endl;
}*/
delete[] hand;
delete[] deck;
return 0;
}
Compiling it works. However, if I remove the /* and */ to add it to the code, it'll still compile, but the program will stop functioning. Windows gives me an error msg that cardDraw.exe has stopped working. Not sure what's going on here..
/* through */ is comment, which I guess you already know since you went through this code. Im not quite sure what you're asking though? The program doesn't work with it or without?
What it's supposed to do is output the value and suit 5 Cards objects in the array hand[]. If the object is a J, Q, K, or Ace it should output that instead of a number.
What happens is the program freezes, and Windows gives me an error (not the compiler).
You're trying to iterate through more cards than there actually are (numCards+1), so you shouldn't be surprised.
By the way, it would be better if you create a Deck class that internally uses a vector to store the cards.
vectors are a basic concept, so if they didn't appear in your book yet, you probably should be worried.
If they appear at all in the book, you might want to read that chapter first. vectors are basically dynamic arrays and are easy to use.