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:

1 Jack of Spades
2 Deuce of Diamonds
3 Three of Diamonds
4 King of Spades
5 Eight of Clubs
6 King of Diamonds
7 Six of Clubs
8 Six of Spades
9 Eight of Hearts

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <ctime> 
#include <string>
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<<card[deck[j][1]]<<" of "<<suit[deck[j][0]]<<endl;
       cout<<endl;
system("pause");
return 0;
}

        
void shuffle(int deck[][2])                  
{bool cards[4][13];
int i,j,num,type;
for(i=0;i<4;i++)
   for(j=0;j<13;j++)
      cards[i][j]=false;            

    for(j=0;j<52;j++)
        {do
           {
            num=rand();
            type=rand()%4;
            }while(cards[type][num]);
         deck[j][0]=type;
         deck[j][1]=num;
         cards[type][num]=true;  
         }
 
return;
}
1
2
3
            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.