# Thread: Problem Putting INTs Into a CHAR Array

1. ## Problem Putting INTs Into a CHAR Array

Hello,

I want to make a char array of a deck of playing cards. I have a 2-d char array called deck[52][2]. where each object in the array has a card value(1-10,J,Q,K,A) and a suit value(S,C,D,H). I made this loop to put the values into the array however i cant get the 1-10 values in because it thinks these are ascii values. My question is how do i make it so that i can put these int values into the char array without having to put them in one by one? Thanks a bunch!

heres my code:

Code:
```for ( int i = 0; i == 51; i++ ) {

if ( i < 13 ) {
deck[i][1] = 'H';
if (i == 0){
int temp = 2;
}
if (temp < 11) {
deck[i][2] = temp;
temp ++;
}
else if (temp == 11){
deck[i][2] = 'J';
temp ++;
}
else if (temp == 12){
deck[i][2] = 'Q';
temp ++;
}
else if (temp == 13){
deck[i][2] = 'K';
temp ++;
}
else if (temp == 14){
deck[i][2] = 'A';
temp = 2;
}
}
if (i > 12 && i < 26) {
deck[i][1] = 'D';
if (temp < 11) {
deck[i][2] = temp;
temp ++;
}
else if (temp == 11){
deck[i][2] = 'J';
temp ++;
}
else if (temp == 12){
deck[i][2] = 'Q';
temp ++;
}
else if (temp == 13){
deck[i][2] = 'K';
temp ++;
}
else if (temp == 14){
deck[i][2] = 'A';
temp = 2;
}
}
if (i > 25 && i < 39) {
deck[i][1] = 'C';
if (temp < 11) {
deck[i][2] = temp;
temp ++;
}
else if (temp == 11){
deck[i][2] = 'J';
temp ++;
}
else if (temp == 12){
deck[i][2] = 'Q';
temp ++;
}
else if (temp == 13){
deck[i][2] = 'K';
temp ++;
}
else if (temp == 14){
deck[i][2] = 'A';
temp = 2;
}
}
if (i > 38 ) {
deck[i][1] = 'S';
if (temp < 11) {
deck[i][2] = temp;
temp ++;
}
else if (temp == 11){
deck[i][2] = 'J';
temp ++;
}
else if (temp == 12){
deck[i][2] = 'Q';
temp ++;
}
else if (temp == 13){
deck[i][2] = 'K';
temp ++;
}
else if (temp == 14){
deck[i][2] = 'A';
temp = 2;
}
}

}```

2. I think you might want to check out THIS thread

3. Hi thanks for the reply, but i having trouble understanding that other thread because i dont know how that "case" stuff works, if possible, could you make it specific to my code would be Extremely helpful, thanks so much!

4. Problem #1:
Code:
`for ( int i = 0; i == 51; i++ ) {`
Your for loop will never execute. The middle statement (in this case i == 51) is the condition. As long as that condition evaluates to true, the loop is run, and when that condition evaluates to false, the loop stops. Since i starts at 0, i == 51 will not be true the first time through and the loop won't ever get run.

Problem #2:
Code:
```if (i == 0){
int temp = 2;
}```
When you define a variable inside of a block of code, that variable is only usable inside that block of code. Here, you define a new variable called temp that goes out of scope and is unusable immediately after the bracket that closes the if statement. If you have another variable called temp that you defined earlier in the code that you didn't post, and you want to set that value to 2, then get rid of the int part of the code above. Otherwise, move the definition to the block of code that you want it to be valid for.

Problem #3: If you want to convert a single digit integer into a character, just do this:
Code:
```if (temp >= 0 && temp < 10)
deck[i][2] = temp + '0';```
This is a trick that uses the fact that the nine characters that represent decimal digits are in order in the ASCII character set. You will have to use 'T' for 10, though, because it is two characters long and you are only storing a single character.

Hope that helps.

5. okay thanks a lot for the reply, so in order to store 10 i would have to do this?

Code:
```else if (temp == 10){
deck[i][2] = T;
temp ++;
}```

6. No, use 'T' instead of T. The first is a character, the second is a name that the compiler won't understand.

7. hmm okay but does that mean when i go to cout that array, will it display a T or a 10?

8. It will display T. The reason you can't use 10 in this instance is because 10 is two characters. You are storing a single character for each card in your deck. You can use anything you want, as long as its a single character. I suggested T because it made more sense to me than any other character.

Once you learn more C++, you can make your program more sophisticated so that it can display 10 instead of T. If you want or have to do that now, then you are going to have to come up with an alternative to storing a single character per card.

9. yah thats what i have to do, any suggestions on how to do that? thanks a bunch

10. It really depends on what you know. My best suggestion is to work on it and figure it out. Since there are dozens of ways to do it, I'm sure you can find one that works.

11. haha dont bet on it, thanks for the help though

12. let me try to use like this:
Code:
`enum deck {J, Q , K , A} cardval;`

13. a short example of switch with your code
Code:
```switch (temp)
{
case 1: (temp < 11);
deck[i][2] = temp;
temp ++;
break;

case 2: (temp == 11);
deck[i][2] = 'J';
temp ++;
break;

}```

remember to put break; or every case after the one that's true will be executed

14. Did a project once that modeled playing poker. It was around 3 or 4 yrs ago, which in college language means recycle your projects . The code for card classes and the deck of cards is posted on my professor's web site. Here's the link. Just look at the card and deck class so you can get an idea of another way to structure your program. Don't just copy it! Study it.

Good luck! If you have to rate the hands at the end of a game, make sure to alot plenty of time and don't let it catch up w/ you. My procratination cost me many hours of sleep

just2peachy

Oh yeah, PS---code is in java