I am studying for my final and we just barely had time to cover pointers and strings, so I'm having some trouble getting the hang of things.
In the following code, I'm supposed to modify the program so that it prints the full names of the cards that you are dealt (e.g. Eight of clubs). It suggests that I do this by replacing rank_code and suit_code by arrays containing pointers to strings.
Code:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_SUITS 4
#define NUM_RANKS 13
int main(void)
{
bool in_hand[NUM_SUITS][NUM_RANKS] = {false};
int num_cards, rank, suit;
const char rank_code[] = {'2','3','4','5','6','7','8',
'9','t','j','q','k','a'};
const char suit_code[] = {'c','d','h','s'};
srand((unsigned) time(NULL));
printf("Enter number of cards in hand: ");
scanf("%d", &num_cards);
printf("Your hand:");
while (num_cards > 0) {
suit = rand() % NUM_SUITS; /picks a random suit */
rank = rand() % NUM_RANKS; /picks a random rank */
if (!in_hand[suit][rank]) {
in_hand[suit][rank] = true;
num_cards--;
printf(" %c%c", rank_code[rank], suit_code[suit]);
}
}
printf("\n");
return 0;
}
From what little I understand, I need to use a pointer array for rank_code similar to
Code:
const char *rank_code[] = {"Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack",
"Queen", "King", "Ace"};
Am I on the right track or is there a lot more to this than I understand?