### Help with simulating a deck of playing cards?

Hey so I am supposed to simulate a deck of playing cards and my program keeps crashing for some reason. Any help would be appreciated. Here are the exact instructions given by my prof:

Write a program to simulate a deck of 52 playing cards.

Represent your deck as a 2D Array where the value in
the cell is the position of the card in the deck.

Represent the names for the suits and faces as
an array of Strings.

Write an algorithm to shuffle the deck.

Display the deck. For Example:

2 Deuce of Diamonds
3 Three of Diamonds
5 Eight of Clubs
6 King of Diamonds
7 Six of Clubs
9 Eight of Hearts

Here is what I have but it keeps crashing. What am I doing wrong?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include #include #include using namespace std; void shuffle(int[][2]); int main() { int j,deck[52][2]; string suit[4]={"hearts","diamonds","spades","clubs"}; string card[13]={"Ace","Deuce","Three","Four","Five","Six", "Seven","Eight","Nine","Ten","Jack","Queen","King"}; srand(time(0)); shuffle(deck); cout<<"deck-after shuffled:\n"; for(j=0;j<52;j++) cout<
 ``123`` `````` num=rand(); type=rand()%4; }while(cards[type][num]);``````

num=rand(); returns a int, which could be a very large number, large enough to exceed the bounds of the array.

 `` `` ``num=rand()%13;``

consider also reading up on the c++11 selection of random engines:
http://www.cplusplus.com/reference/random/
Last edited on
Topic archived. No new replies allowed.