Note that there are some explanatory texts on larger screens.

plurals
  1. POInputing characters into arrays in C- follow up
    text
    copied!<p>This is a follow up question to a previous question I asked about calculating a straight hand..not exactly the same...This is the method to read in cards- it works- but is there a better way- to do this using console input in C ...</p> <pre><code>void read_cards(int num_in_rank[], int num_in_suit[]) { bool card_exists[NUM_RANKS][NUM_SUITS]; char ch, rank_ch, suit_ch; int rank, suit; bool bad_card; int cards_read = 0; for (rank = 0; rank &lt; NUM_RANKS; rank++) { num_in_rank[rank] = 0; for (suit = 0; suit &lt; NUM_SUITS; suit++) card_exists[rank][suit] = false; } for (suit = 0; suit &lt; NUM_SUITS; suit++) num_in_suit[suit] = 0; while (cards_read &lt; NUM_CARDS) { bad_card = false; printf("Enter a card: "); rank_ch = getchar(); switch (rank_ch) { case '0': exit(EXIT_SUCCESS); case '2': rank = 0; break; case '3': rank = 1; break; case '4': rank = 2; break; case '5': rank = 3; break; case '6': rank = 4; break; case '7': rank = 5; break; case '8': rank = 6; break; case '9': rank = 7; break; case 't': case 'T': rank = 8; break; case 'j': case 'J': rank = 9; break; case 'q': case 'Q': rank = 10; break; case 'k': case 'K': rank = 11; break; case 'a': case 'A': rank = 12; break; default: bad_card = true; } suit_ch = getchar(); switch (suit_ch) { case 'c': case 'C': suit = 0; break; case 'd': case 'D': suit = 1; break; case 'h': case 'H': suit = 2; break; case 's': case 'S': suit = 3; break; default: bad_card = true; } while ((ch = getchar()) != '\n') if (ch != ' ') bad_card = true; if (bad_card) printf("Bad card; ignored.\n"); else if (card_exists[rank][suit]) printf("Duplicate card; ignored.\n"); else { num_in_rank[rank]++; num_in_suit[suit]++; card_exists[rank][suit] = true; cards_read++; } } } </code></pre> <p>I know the case statement can have a few optimizations-i.e. using toupper; better representation the card values, etc..Suggestions- please. The code is reflective of C99...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload