Hey everyone,
I wrote some code to create a deck of cards, described the deck and randomly deals 5 cards. First, I want to let everyone know that this is homework for my C-II college class, so I am not looking for any answers, but solutions or guidance in figuring out what I am trying to do. What I want to do with this deck of cards is to shuffle the deck and then deal 5 cards, while not dealing the same cards in that hand.
IE:
Program deals 5 cards. The 5 cards are as follow (I actually ran the program to get this scenario).
Your card is 6 of Spades. <-- This card is identical to the one below.
Your card is 11 of Diamonds.
Your card is 9 of Diamonds.
Your card is 6 of Spades. <-- Identical card to the one above.
Your card is 13 of Spades.
/IE
So, as stated above, how can I fix this while shuffling, randomly dealing, and only producing 1 card each in a hand? Any help will be appreciated. Thanks!
P.S. Almost forgot, here's my code.
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <string.h>
struct deck{
int pips;
char suit;
}deck[52];
main()
{
int i;
int j;
srand(time(NULL));
char C[6]="Clubs";
char D[9]="Diamonds";
char H[7]="Hearts";
char S[7]="Spades";
for(i=0; i<13; i++)
{ deck[i].pips=i+1;
deck[i+13].pips=i+1;
deck[i+26].pips=i+1;
deck[i+39].pips=i+1;
deck[i].suit='C';
deck[i+13].suit='D';
deck[i+26].suit='H';
deck[i+39].suit='S';
}
for(i=0;i<5;i++)
{
j=rand()%53;
printf("Your card is %d of ", deck[j].pips);
switch(deck[j].suit)
{
case 'C': printf("Clubs\n");break;
case 'D': printf("Diamonds\n");break;
case 'H': printf("Hearts\n");break;
case 'S': printf("Spades\n");break;
default : printf("Out of bounds.\n");break;}
}
system("PAUSE");
}