0

so I have created a (mostly working) poker hand score meter, the only problem I am currently having is that I need to sort the values of the card into order to correctly identify a straight or similar.

Currently my program returns foreign keys where the sorted integer amounts are meant to be displayed.

My current input: 6D 7D 9D 8C 5D

My current output:

  Six of diamonds
  Seven of diamonds 
  Nine of diamonds 
  Eight of clubs 
  Five of diamonds 
  � <--- (This is where I have told it to display the Numbers in order)
  High Card! 1 point.

Basically I know my scoring counts are correct, just that the sorting is not working.

My Code

  #include <stdio.h>

int main( void )
{
 char cardval[10] = {'\0'}, count;
 char maincard[2] = {'\0'};
 int intval[5], i;
 int tempval;
 int dupecount = 0;
 int suitcount = 0;
 int a;


 for(count=0;count<10;count++)
 {   
  scanf(" %c", &(cardval[count] ) );
  scanf(" %d", &(intval[i]) );
  i++;
  count++;
  scanf(" %c", &(cardval[count] ) );

  strcpy( maincard, &cardval[count-1] );
  strcat( maincard, &cardval[count] ); 
  switch (maincard[0])
  {
   case '2':
      printf ( "Two of ");
   break;
   case '3':
      printf ( "Three of ");
   break;
   case '4':
      printf ( "Four of ");
   break;
   case '5':
      printf ( "Five of ");
   break;
   case '6':
      printf ( "Six of ");
   break;
   case '7':
      printf ( "Seven of ");
   break;
   case '8':
      printf ( "Eight of ");
   break;
   case '9':
      printf ( "Nine of ");;
   break;  
   case 'J':
      printf( "Jack of ");
   break;
   case 'Q':
      printf( "Queen of ");
   break;
   case 'K':
      printf( "King of ");;
   break;
   case '0':
      printf( "Ten of ");
   break;
   case 'A':
      printf( "Ace of ");
   break;
   default:
      printf( "\n %c is an incorrect input, please enter a number 0, 2-9, A,K,Q,J", maincard[0]);
      return 0;
   break;
  } 
  switch (maincard[1])
  {
   case 'D':
      printf( "diamonds");
   break;
   case 'S':
      printf( "spades");
   break;
   case 'H':
      printf( "hearts");
   break;
   case 'C':
      printf( "clubs");
   break;
   default:
      printf( "\n %c is an incorrect input, please enter D, S, H, or C as a suit", maincard[1]);
      return 0;
   break;
  }
  printf ("\n");
 }
 for ( a=0; a<5; a++ )
 {
  for ( i=0; i<5-1; i++)
   {
     if ( intval[i] > intval[i+1])
      {
       tempval = intval[i];
       intval[i] = intval[i+1];
       intval[i+1] = tempval;
       tempval = intval [i];
      }
  }
}
printf("%c", intval);

What is wrong with my sort section of the code? it seems that the intval is not being recorded correctly.

2 Answers2

1

intval is an array of 5 ints. So the following is wrong:

printf("%c", intval);

To print the array, use a loop:

for(i=0;i<5;i++)
printf("%d ", intval[i]);

i is an unintialized when you read intval[i] using scanf.

  scanf(" %d", &(intval[i]) );

Initialize it to 0.

int intval[5], i = 0;
P.P
  • 117,907
  • 20
  • 175
  • 238
0

Try this.

if ( intval[i] > intval[i+1])
  {
   tempval = intval[i+1];
   intval[i+1] = intval[i];
   intval[i]=tempval; 
  }
Ivo
  • 450
  • 3
  • 18