Inputing characters into arrays in C- follow up -


This is a follow-up question to a previous question which I asked about calculating a straight hand ... not the same. .. This is the way to read in the card - it works - but this is a better way - to use console input in C. ...

  zero read_cards (int num_in_rank [], Int num_in_suit []) {bool card_exists [NUM_RANKS] [NUM_SUITS]; Four f, rank_text, suit_tech; Int rank, suit; Bull badcard; Int card_read = 0; {Num_in_rank [rank] = 0; for (rank = 0; rank & lt; NUM_RANKS; rank ++); (Suite = 0; Suit & lt; NUM_SUITS; Suit ++) [ranks] [suit] = Wrong for card_exists; } (Suite = 0; Suit & Lt; NUM_SUITS; Suit ++) num_in_suit [Suite] = 0; While (card_read and lt; NUM_CARDS) {bad_card = false; Printf ("Enter a Card:"); Rank_click = by (); 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 'A': Case 'K': Rank = 11; break; Case 'A': Case 'A': Rank = 12; break; Default: bad_card = true; } Suit_ch = meet (); Switch (suit_ch) {case 'C': case 'C': Suit = 0; break; Case 'D': Case 'D': Suit = 1; break; Case 'H': Case 'H': Suit = 2; break; Of the case ': 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"); And if (card_exist [rank] [suit]) printf ("duplicate card, neglected. \ N"); Else {num_in_rank [rank] ++; Num_in_suit [suite] ++; Card_exists [rank] [suite] = true; Cards_read ++; }}}  

I know that there may be some customization in the case statement - like Using Toupper; Better card value, etc. Suggestion - Please reflect the code C99 ...

The code looks ok to me Has been quite well and readable in my opinion.

It seems that you are asking about optimization - unless you are sure that you need it, otherwise it is less edible than your text and more bugs. Specifically, this function is going to spend more than 99.99%, which is waiting for the user to type now, so there is no need to customize it.

I do not think it is necessary to use the fall-thru to hold both upper and lowercase inputs in the switch statement, especially if you have some cases with just a few more Is typing.

If this is indeed an issue with code and whether it is not compiling or not, then what is this, please describe the problem.

Edit: The only thing you want to change is to get rid of the obvious cases for the 0-9 rank, and instead in the case of the body, all of them combine in the same fall:

  rank = rank_ch - 0 ';  

This is a relatively common idiom for converting a stored number in the form of a character, so it should be readable to other programmers.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -