-4

So i am trying to create a C code program that will be given the shorthand notation for a playing card and will determine the formatted playing card. Example Input: H 8 Output: 8 of Hearts

    Input: C 14
    Output: Ace of Clubs

Rank: 2-10, 11 Jack, 12 Queen, 13 King, 14 Ace Suit: C Clubs, D Diamonds, H Hearts, S Spades But in the final stages of implementation i ran into a couple serious problems. The program runs fine when I enter like D 5 but enter D 12 will make it list queen then jack then 12 of diamonds.

Heres the code: http://pastebin.com/Tj4m6E2L And heres the current EXE: http://www.mediafire.com/download/4fy4syga2aj8n2j

Thanks for any help you can provide. I am new to C code so keep it simple and stupid for my benefit.

  • 3
    provide your code here, its easier to find problems and answer – Haris Oct 13 '14 at 16:04
  • Edit to include what is your problem with your code, and what you have tried. This isn't 'code review' – JohnH Oct 13 '14 at 16:04
  • The spec in the code comments show an example input as `Input=5C` -- you have to read that in as a string, one time, rather than have two inputs, if it is really specified that way. – JohnH Oct 13 '14 at 16:06
  • You need to read up on the `break` statement and then sprinkle these liberally throughout that huge sprawling `switch`. While you're doing that, try to think about how you might simplify your code - there's a huge amount of repetition and redundancy. – Paul R Oct 13 '14 at 16:09
  • Ask yourself if you were to happen upon this question "is there is enough information to respond with anything helpful?" Show your effort. Point out the specific error you are seeing. Ask specific question, i.e. what do you need help with? – ryyker Oct 13 '14 at 16:09

2 Answers2

0

You're missing crucial break statements in your switch, e.g.

switch(rank)
{
  case 14:
   {
   if(suite == 'H')
        printf("Your card is the Ace of Hearts!");
   else if(suite == 'C')
        printf("Your card is the Ace of Clubs!");
   else if(suite == 'D')
        printf("Your card is the Ace of Diamonds!");
   else
        printf("Your card is the Ace of Spades!");
   }
  // <<< NB: case 14 "falls through" to case 13 here !!!
  case 13:
    ...

Change this to:

switch(rank)
{
  case 14:
   {
   if(suite == 'H')
        printf("Your card is the Ace of Hearts!");
   else if(suite == 'C')
        printf("Your card is the Ace of Clubs!");
   else if(suite == 'D')
        printf("Your card is the Ace of Diamonds!");
   else
        printf("Your card is the Ace of Spades!");
   }
   break; // <<< FIX
  case 13:
    ...

Repeat for all the other missing break statements.

Paul R
  • 208,748
  • 37
  • 389
  • 560
0

You should always put a break; statement at the end of every case in the switch statement. Without it, if a certain case is met, all the lines that appear after that case will be executed.

For example, the first case in your program should be written as followed:

case 14:
     { //not required
   if(suite == 'H')
        printf("Your card is the Ace of Hearts!");
   else if(suite == 'C')
        printf("Your card is the Ace of Clubs!");
   else if(suite == 'D')
        printf("Your card is the Ace of Diamonds!");
   else
        printf("Your card is the Ace of Spades!");
     } //not required
   break; //add this line

Also, you may omit the braces as they are not required.

Yoni
  • 1
  • 1
  • 2