help with shuffling a deck

Hi, im new to code and have a hard time learning it. im trying to shuffle a deck a cards, but when it shuffles it, i get a negative number , one of functions is problem. the output should be first in order, 101,102,103,104..... but when the call shuffle function i should have the same numbers but shuffled. but it has a negative n here is my code any suggestions

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
const int deck_Size=52;
void initializeDeck( int deck[]);
void dumpDeck(const int deck[], int size);
int getRandomNumber(int low, int high);
void shuffle(int deck[], int size);
void showCard(int card);
void showCards( const int cards[], int numCards ); //bool hideFirstCard put it back

void main()
{
int deck[deck_Size];

initializeDeck (deck);
dumpDeck(deck,deck_Size);
shuffle(deck,deck_Size);
showCard(104);
showCards( deck, deck_Size );



cin.get();
cin.ignore();
}


void initializeDeck( int deck[])
{
int index=0;

for (int x=100; x<=400; x=x+100)
{
for (int y=1; y<=13;y++)
{
deck[index]=y+x;
index++;
}
}
}



void dumpDeck(const int deck[], int size)
{
for ( int index=1; index <=size; index++)
{
cout<< deck[index-1];
cout<<endl;
}


}





int getRandomNumber(int low, int high) {
static bool firstTime=true;
int randNum;

//if first time called, seed random number generator
if (firstTime) {
srand( static_cast<unsigned int>(time(NULL)) );
firstTime=false;
}

//generate random number between given low and high values (inclusive)
randNum = rand() % (high-low+1) + low;

return randNum;
}

void shuffle(int deck[], int size)
{


int randNum1=0;
int randNum2=0;
int index=0;
int holdcard=0;

for( index=0; index<=500; index++)
{
randNum1 = getRandomNumber(0, 52);
randNum2 =getRandomNumber(0, 52);
holdcard= deck[randNum1];
deck[randNum1]= deck[randNum2];
deck[randNum2] = holdcard;

}

}

void showCard(int card)
{
int rank=0;
char suit=0;
if (card <=0)
{
}

if (card<=113)
{
rank= card-100;
suit = static_cast<char>(3);
}
else if (card<=213)
{
rank =card -200;
suit= static_cast<char>(4);
}
else if (card<=313)
{
rank=card-300;
suit = static_cast<char>(5);
}
else if (card<=413)
{
rank=card-400;
suit =static_cast<char>(6);
}

switch(rank)
{
case 1:
{

rank='A';

break;
}
case 11:
{
rank= 'J';
break;
}
case 12:
{
rank='Q';
break;
}
case 13:
rank ='K';

}
if( rank== 'A' || rank== 'J' || rank == 'Q'|| rank == 'K')
{
cout << static_cast<char> (rank)<<suit<<endl;
}
else
{
cout << rank<<suit<<endl;
}
}


void showCards( const int cards[], int numCards )
{
int index=0;
for(index=0; index<numCards;index++)
{
//cout<< cards[index]<<endl;
showCard(cards[index]);
}




}
Last edited on
The problem is in getRandomNumber
1
2
3
 
  randNum1 = getRandomNumber(0, 52);
  randNum2 =getRandomNumber(0, 52);


Max array index is 51.
oh yeah i forgot about that. thanks a lot
Topic archived. No new replies allowed.