I am having a problem with the end of my program. This program is designed to pick 100 playing cards at random, and print out the end results, for example, how many jacks, queens, etc. The problem i am having is trying to print out the results color of the cards (for example red = diamonds and hearts, black = clubs, spades). What it is suppose to print out is '"You were dealt _ amount of red cards and _ amount of black cards", but it shows the sentence twice, and the numbers do not add up to 100, the number of andom picks (p.s. I tried to include the indents so it can be easier to read, but i was having a hard time trying to format this email):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int cards, numberOnCard, cardRank[14] = {0}, cardSuit[5] = {0}, kindOfCard,
totalBlack = 0, totalRed = 0;
srand(time(NULL));
/* Picking 100 cards at random */
for (cards=1; cards<=100; cards++)
{
numberOnCard = rand() % 13 +1;
kindOfCard = rand() % 4 + 1;
cardRank[numberOnCard]++;
cardSuit[kindOfCard]++;
/* "numberOnCard" is the number or face on the card */
switch (numberOnCard)
{
case 1:
printf("Card # %d = Ace ", cards);
break;
case 2: case 3: case 4: case 5: case 6: case 7: case 8:
case 9: case 10:
printf("Card # %d = %d ", cards, numberOnCard);
break;
case 11:
printf("Card # %d = Jack ", cards);
break;
case 12:
printf("Card # %d = Queen ", cards);
break;
case 13:
printf("Card # %d = King ", cards);
break;
}
/* "kindOfCard" is the face/suit of the card */
switch (kindOfCard)
{
case 1:
printf("of Spades\n");
break;
case 2:
printf("of Clubs\n");
break;
case 3:
printf("of Hearts\n");
break;
case 4:
printf("of Diamonds\n");
break;
}
/* end for random pick */
}
/* Printing out how many of each rank was chosen */
for (numberOnCard=1; numberOnCard<=14; numberOnCard++)
{
switch (numberOnCard)
{
case 1:
printf("\nYou were dealt an Ace card %d times.\n",
cardRank[numberOnCard]);
break;
case 2: case 3: case 4: case 5: case 6: case 7: case 8:
case 9: case 10:
printf("You were dealt a %d card %d times.\n",
numberOnCard, cardRank[numberOnCard]);
break;
case 11:
printf("You were dealt a Jack %d times.\n",
cardRank[numberOnCard]);
break;
case 12:
printf("You were dealt a Queen %d times.\n",
cardRank[numberOnCard]);
break;
case 13:
printf("You were dealt a King %d times.\n",
cardRank[numberOnCard]);
break;
}
}
for (kindOfCard=1; kindOfCard<=4; kindOfCard++)
{
if (kindOfCard == 1 || kindOfCard == 2)
{
totalBlack = cardSuit[kindOfCard] + cardSuit[kindOfCard];
printf("You were dealt %d black cards ", totalBlack);
}
else if (kindOfCard == 3 || kindOfCard == 4)
{
totalRed = cardSuit[kindOfCard] + cardSuit[kindOfCard];
printf("and %d red cards.\n ", totalRed);
}
}
return 0;
}