Advertisement

Arrays of strings and division

Started by August 02, 2002 09:26 AM
6 comments, last by sanguineraven 22 years, 3 months ago
I am pretty new to C++ and have read through a number of tutorials and have started by creating a simple text blackjack game. I have a variable CARD_NUMBER which a number between 1 and 52 is placed in. I have a second variable called SUIT_NAME which is a string as declared by char SUIT_NAME[9]; This will hold Diamonds,Hearts etc. Now, one way I could get the name of the suit is by dividing CARD_NUMBER by 13 to get a nuumber between 0 and 3. Then just use a case/switch. However, I came up with a different way. I want an array of strings. The 4 positions will be assigned as Diamonds, Hearts etc. then I can do SUIT_NAME = SUITS[CARD_NUMBER/13]; This should work but here is where the two questions are: 1. Since a string is an array of characters, can you, and how do you make an array of strings. 2. I know in pascal there was the operator / which would just do division, possibly resulting in a real number and you could do DIV which would always return an integer. How would I get it to return an integer so when i did the division by 13 I would get an integer 0 1 2 or 3? Thanx for any help, it is greatly appreciated What about me? What about Raven?
Alright to answer your questions:
1. For your array of strings you can call it by doing this:
char arraystring[characterNumber][stringNumber] //creates a 2d array 

or the dynamic approach:
char **cards //creates a 2d array 

and basically, you would deal with it that way.
2. For your second question, C++ will do integer division by default. To do floating point division, you have to: a. user floats for the division, b. assign the result to a floating point numbers.I hope that this helps you.
Advertisement
Thankyou for your reply. Yeah I think it helps, so now I''ve made
char SUITS[9][4];
which holds the 4 different suits in 0 1 2 3 so when I assign a string, say Diamonds to the first slot what do I do I do?
SUITS[?][0] = "Diamonds"; what goes in there?

P.S I think the tutorial page on www.gametutorials.com has been hax0red

What about me? What about Raven?
Thankyou for your reply. Yeah I think it helps, so now I''ve made
char SUITS[9][4];
which holds the 4 different suits in 0 1 2 3 so when I assign a string, say Diamonds to the first slot what do I do I do?
SUITS[?][0] = "Diamonds"; what goes in there?

P.S I think the tutorial page on www.gametutorials.com has been hax0red

What about me? What about Raven?


Don''t make SUITS[9][4]. You would want to make SUITS[4][9].

Then SUITS[0] would be a pointer to the first name and SUITS[1] would be a pointer to the second name, etc.

And as said before, if you divide with ints you will always get an int value returned.

For future reference this function may be useful.

itoa();

Here is how you would use it.



          int number = 0;	char string[10];	itoa(number,string,sizeof(string));        //string now stores the ascii representation of 0  


So basically the function takes as arguements the interger you want to convert, the string you want to put it into (a reference to the string), and then the size of memory to be allocated for the new string.

Feel free to email me at NYYanks432@hotmail.com if you have any questions
Feel free to email me at NYYanks432@hotmail.com if you have any questions
The third argument of itoa is the radix, not the length of the string.

That example is a bit lucky, because sizeof(char[10]) == 10 which is the base most humans use.

So you can use it to convert to hex with a radix of 16, or binary with a 2.
Advertisement
AP, (or anyone else for that matter), I''m not sure exactly what you mean. Could you explain to me what is happening and what I need to do to assure that this isn''t just a lucky incident.

Thanks

Feel free to email me at NYYanks432@hotmail.com if you have any questions
Feel free to email me at NYYanks432@hotmail.com if you have any questions
"radix" is the BASE of the number. Decimal is base (or radix) 10. Hexadecimal is "radix" 16. Binary is radix 2. Octal is radix 8. Ya see?



Don''t listen to me. I''ve had too much coffee.

This topic is closed to new replies.

Advertisement