By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

Array of strings in C?

P: 2
Hi.. I need to know how to assign a value to each string in my array, if that can be done. I initialised a deck of cards by using

char *deck[52] = { "Ace of Hearts", "Two of Hearts, ........., "King of Spades" };

and now I need to find a way of assigning a value to each string so I can use them for calculation (I'm writing a simple Blackjack game in C, and I need the values of each card to calculate the total points of each player). Ex, "Two of Hearts" = 2, "King of Spades" = 10, "Ace of Hearts" = 11, ..........

Please help me. I'm new to C, and need to write that blackjack program for next week. I'm lost!

Thanks!
Nov 15 '08 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
Instead of using an array of c-strings to store the deck, why not make a simple Card struct? Each card has a number (Ace, Six, Jack, Ten, etc.), a suit (Diamonds, Hearts, Spades, or Clubs), and a value (1, 2, 3, etc...). Now you can make an array of Cards to be your deck.
Nov 15 '08 #2

P: 2
Instead of using an array of c-strings to store the deck, why not make a simple Card struct? Each card has a number (Ace, Six, Jack, Ten, etc.), a suit (Diamonds, Hearts, Spades, or Clubs), and a value (1, 2, 3, etc...). Now you can make an array of Cards to be your deck.
Hi, and thanks for your reply.. But I'm very very very confused about C programming. I always seem to confuse everything.. So I don't know how to combine these 3 arrays so I can use them.
That is why I wanted to use only one array, but I think I'm making it harder myself.
Also, I don't know how to randomise my 52 cards so that I don't get any duplicates. Ex: I can have Ace four times, Diamonds 13 times, but not twice of an Ace of Diamonds!

And please help me on how to assign and use the values (Two of Hearts = 2)...
==Thanks==
Nov 16 '08 #3

Banfa
Expert Mod 5K+
P: 8,916
So I don't know how to combine these 3 arrays so I can use them.
They aren't arrays they are members of a structure, however I personally think that only 2 members are required, number (Ace,2,3, ... Jack, Queen, King) and suit (Hearts, Diamonds, Spades, Clubs).

For the purposes of black jack the suit is irrelevant and there is a direct mapping of number onto value of the card in a game of black jack apart from the Ace which will require a little special handling.

Do you know how to define a structure?
Nov 16 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.