Need assistance with creating a deck and shuffling it

I am new to c++ and I need help on what i did wrong with this code. I was trying to create a deck and print it and shuffle it and then print it again but i can't see what i did wrong. Please help.


#include <iostream>
#include <string>
#include <time.h>
#include <math.h>
#include <stdio.h>


using namespace std;


void initDeck (string ideck[][52]);
void shuffleDeck (string sDeck);

int main()
{

string sDeck;
string ideck[4][52];
int card;
//int ideck[4][13] = {0};


initDeck (ideck);
shuffleDeck();
for(int i= 0; i<+5; i++)
{
printf("%i%s", card[i].card << card[i],suit << endl;
}



system("pause");
}
void initDeck (string ideck[])
{
string suit[4] = {"\x03", "\x04", "\x05", "\x06"};
string rank[13] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K","A"};



int i = 0;
int j = 0;
cout << "Initial Deck" << endl;
for (i = 0; i < 52; i++)
for (j = 0; j < 4; j++)

cout << "Rank:" << rank[i] << "\t" << "Suit:" << suit[j] << endl;
}
void shuffleDeck (void)
{
int i = 0;
while( i < 100)
{
int x = rand()%52+1;
int y = rand()%52+1;
card[0] = card[y];
card[y] = card[x];
card[x] = card[0];
i++;
}
}
I didn't read all of your code but, you could make your card deck a vector or another STL container and use this:
http://www.cplusplus.com/reference/algorithm/random_shuffle.html
Your iDeck is a 4x52 array. Unless you mean it to be 4 decks of cards, it should only be a single array of 52 cards (or, as eker said, a vector).

Your initDeck call doesn't actually set anything in iDeck. Your for(i = 0... is using an incorrect range. You only have 13 elements in rank, not 52.

Neither of your function declarations matchs your function definitions.

You should seed srand() before using rand().

Your shuffleDeck function isn't going to work. When you declare an array of 52 elements, it has 52 elements. The first element is 0, the last element is 51. IF you want to use element zero as your temporary card for shuffling, you need to declare an array of 53 elements total. You also have to change all your for loops. You're better off using a local temp variable though (or again, as eker said, vector).

Your printf line looks like you tried morphing it into a cout call. Printf isn't Polymorphic though. :-P

There are possibly other syntax errors as well.
Topic archived. No new replies allowed.