Home > Blockchain >  assigning a letter to an int variable in c
assigning a letter to an int variable in c

Time:11-28

Trying to generate a deck of cards, and the face values for the deck are ints. So obviously, for 2-10, the values would just be the numbers 2-10, pretty simple. If the face value was 3, I would just assign it like:

card->face = 3;

my problem lies in aces, jacks, kings and queens. The face value always has to be an int, but A, J, K, and Q are characters, so I'm not sure how I would assign these. Is there a way using ASCII values?

CodePudding user response:

char c ;
scanf("%c",&c);
if(c >49 && c < 58 ){
  int a = c - 48 ;
  printf("%d",a);
}
else {
  printd("%c",c) ;
}

CodePudding user response:

In C, characters are represented by the small integers which are their values in the machine's character set. And in ASCII, which is the character set which virtually all machines these days are based on, values 1-10 don't correspond to printable characters. So it would be possible to both say

card->face = 3;

to set a value of 3, and

card->face = 'K';

to set the king. This would result in values 2-10 for 2-10, and values 65, 74, 81, and 75 for the Ace, Jack, Queen, and King, respectively.

You would have a small problem, though, when it came to printing these values back out. If you were using printf, then for numeric values you could print them using %d, but for the face cards and the Ace you would have to print them using %c. So your printing code might look like

if(card->face <= 10)
     printf("%d", card->face);
else printf("%c", card->face);

The other possibility, as suggested in the comments, would be to store values using ordinary integers 1-13, then take care of printing special faces using a switch statement. That might look like this:

switch(card->face) {
    case 1:
        printf("A");
        break;

    case 2: case 3: case 4: case 5: case 6:
    case 7: case 8: case 9: case 10:
        printf("%d", card->face);
        break;

    case 11:
        printf("J");
        break;

    case 12:
        printf("Q");
        break;

    case 13:
        printf("K");
        break;
}

Yet another way would be to define an array of strings containing the face "names":

const char *facenames[] = {"A", "2", "3", "4", "5", "6", "7",
                           "8", "9", "10", "J", "Q", "K"};

and then print them like this:

printf("%s", facenames[card->face]);

In choosing between these methods, the other question would be, how are values for card->face going to be input? If a user is going to be typing them in, you're going to need code to detect whether the input is a number or a letter, and handle them both correctly. Or, if your code is going to generate values (perhaps you're going to generate a randomly shuffled deck of cards), it might need to have some special cases also.

There is almost a third possibility for storing the faces. Rather than using integers 2-10 for the numeric cards, and letters 'J', 'Q', and 'K' for the face cards, you could almost get away with using characters '2', '3', '4'… for the numeric cards also. That would make printing them out very simple, just

printf("%c", card->face);

with no if statements, switch statements, or facenames arrays. But the problem (the fatal problem) is that in that scheme there's no easy or obvious way to represent the 10. (Unicode has characters ⑩ , ⑽ , ⒑, and

  • Related