# Sorting this in C language.

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 01-27-2013
c99tutorial
I already mentioned a solution. Here:

valueAmount
suitAmount

add two members of type `int' to your CARD structure. These should just store a plain integer that represents how valuable (numerically) a certain card is. For example, if it's a five of clubs, you could say valueAmount is 5 (i.e. the card with face value "5" has a numerical value of 5) and suitAmount is 4 (i.e. Clubs has a numerical value of 4-- I think this one is really up to you, I'm not an expert on cards)
• 01-27-2013
MF1991
Something like this?

Code:

```int valueAmount[13];    valueAmount[0] = "2";  valueAmount[1] = "3";  valueAmount[2] = "4";  valueAmount[3] = "5";  valueAmount[4] = "6";  valueAmount[5] = "7";  valueAmount[6] = "8";  valueAmount[7] = "9";  valueAmount[8] = "0";  valueAmount[9] = "11";  valueAmount[10] = "12";  valueAmount[11] = "13";  valueAmount[12] = "14";    int valueSuit[4];    valueSuit[0] = "1";  valueSuit[1] = "2";  valueSuit[2] = "3";  valueSuit[3] = "4";```
• 01-27-2013
c99tutorial
"2", "3", "4", etc are not integers. 2, 3, 4 are integers. Also, make them part of your CARD struct, i.e.

Code:

```cards[i].valueAmount = 2; // etc...```
• 01-27-2013
MF1991
Like this?

[code]

case '2':
sprintf(cards[i].value, "Two");
cards[i].valueAmount[0] = 2;
break;

case '3':
sprintf(cards[i].value, "Three");
cards[i].valueAmount[1] = 3;
break;

[\code]
• 01-27-2013
c99tutorial
It depends on how you define valueAmount. Rather than ask if it's correct, why not try it out first and see if it works? Compile with warnings turned on and fix them first. If you need help fixing an error or warning, post that information here and you will be more likely to get something helpful.
• 01-27-2013
MF1991
Code:

```#include <stdio.h> #include <stdlib.h> #include <string.h>    typedef struct {   char* shortValue;   char shortSuit[1];   char value[20];   char suit[20];  } CARD;    CARD cards[5];  int score;  int failed;    int valueAmount[13];    valueAmount[0] = 2;  valueAmount[1] = 3;  valueAmount[2] = 4;  valueAmount[3] = 5;  valueAmount[4] = 6;  valueAmount[5] = 7;  valueAmount[6] = 8;  valueAmount[7] = 9;  valueAmount[8] = 10;  valueAmount[9] = 11;  valueAmount[10] = 12;  valueAmount[11] = 13;  valueAmount[12] = 14;    int valueSuit[4];    valueSuit[0] = 1;  valueSuit[1] = 2;  valueSuit[2] = 3;  valueSuit[3] = 4;    int main(void){    /* Putting space between the lines makes it look "Tidy" or "Professional" */       printf("Welcome to Poker Game.\n\n");       printf("Lets Start.\n\n");       printf("Draw Your Cards.\n\n");       for(int i = 0; i < 5; i++){       cards[i].shortValue = calloc(1, sizeof(char));         }     scanf("%c%c %c%c %c%c %c%c %c%c", cards[0].shortValue, cards[0].shortSuit, cards[1].shortValue, cards[1].shortSuit, cards[2].shortValue, cards[2].shortSuit, cards[3].shortValue, cards[3].shortSuit, cards[4].shortValue, cards[4].shortSuit);     /* Use of switch command for long if statement */   /* Use value of 0 as Ten */   /* For each card in input, run check to determine number/letter in word form */       for(int i = 0; i < 5; i++){           switch(cards[i].shortValue[0])       {               case '2':           sprintf(cards[i].value, "Two");           cards[i].valueAmount[0] = 2;           break;                                   case '3':           sprintf(cards[i].value, "Three");           cards[i].valueAmount[1] = 3;           break;                                   case '4':           sprintf(cards[i].value, "Four");           cards[i].valueAmount[2] = 4;           break;                                 case '5':           sprintf(cards[i].value, "Five");           cards[i].valueAmount[3] = 5;           break;                                 case '6':           sprintf(cards[i].value, "Six");           cards[i].valueAmount[4] = 6;           break;                                 case '7':           sprintf(cards[i].value, "Seven");           cards[i].valueAmount[5] = 7;           break;                                   case '8':           sprintf(cards[i].value, "Eight");           cards[i].valueAmount[6] = 8;           break;                     case '9':           sprintf(cards[i].value, "Nine");           cards[i].valueAmount[7] = 9;           break;                     case '0':           sprintf(cards[i].value, "Ten");           cards[i].valueAmount[8] = 10;           break;                                 case 'J':           sprintf(cards[i].value, "Jack");           cards[i].valueAmount[9] = 11;           score += 1;           break;                               case 'Q':           sprintf(cards[i].value, "Queen");           cards[i].valueAmount[10] = 12;           score += 1;           break;                                 case 'K':           sprintf(cards[i].value, "King");           cards[i].valueAmount[11] = 13;           score += 1;           break;                   case 'A':           sprintf(cards[i].value, "Ace");           cards[i].valueAmount[12] = 14;           score += 1;           break;                       default:             printf("Invalid value character for card #%d. \n", i+1);             failed = 1;             break;         }     }       /* Run check for all short suits in input */     for(int i = 0; i < 5; i++)   {       switch(cards[i].shortSuit[0])       {               case 'C':         sprintf(cards[i].suit, "Clubs");         cards[i].valueSuit[0] = 1;         break;                         case 'D':         sprintf(cards[i].suit, "Diamonds");         cards[i].valueSuit[1] = 2;         break;                         case 'H':         sprintf(cards[i].suit, "Hearts");         cards[i].valueSuit[2] = 3;         break;                         case 'S':         sprintf(cards[i].suit, "Spades");         cards[i].valueSuit[3] = 4;         break;                         default:         printf("Invalid suit character for card #%d.\n", i+1);         failed = 1;         break;       }   }       if(failed == 1){       printf("Invalid value or suit character. Program exiting.\n");       return 0;   }       for(int i = 0; i < 5; i++)       printf("%s of %s\n", cards[i].value, cards[i].suit);             printf("\n"); }```
i keep getting duplicate error for valueAmount
• 01-27-2013
c99tutorial
Quote:

Originally Posted by MF1991
Code:

```#include <stdio.h> #include <stdlib.h> #include <string.h>    typedef struct {   char* shortValue;   char shortSuit[1];   char value[20];   char suit[20];  } CARD; /*...*/           case '2':           sprintf(cards[i].value, "Two");           cards[i].valueAmount[0] = 2;           break;```
i keep getting duplicate error for valueAmount

valueAmount should be a member of your CARD structure. Also it's not an array so you don't need brackets.
• 01-27-2013
MF1991
Code:

```#include <stdio.h> #include <stdlib.h> #include <string.h>    typedef struct {   char* shortValue;   char shortSuit[5];   char value[20];   char suit[20];   char valueAmount[13];    } CARD;  case '2':           sprintf(cards[i].value, "Two");           cards[i].valueAmount[0] = 2;           break;                                   case '3':           sprintf(cards[i].value, "Three");           cards[i].valueAmount[1] = 3;           break;                                   case '4':           sprintf(cards[i].value, "Four");           cards[i].valueAmount[2] = 4;           break;                                 case '5':           sprintf(cards[i].value, "Five");           cards[i].valueAmount[3] = 5;           break;                                 case '6':           sprintf(cards[i].value, "Six");           cards[i].valueAmount[4] = 6;           break;                                 case '7':           sprintf(cards[i].value, "Seven");           cards[i].valueAmount[5] = 7;           break;                                   case '8':           sprintf(cards[i].value, "Eight");           cards[i].valueAmount[6] = 8;           break;                     case '9':           sprintf(cards[i].value, "Nine");           cards[i].valueAmount[7] = 9;           break;                     case '0':           sprintf(cards[i].value, "Ten");           cards[i].valueAmount[8] = 10;           break;                                 case 'J':           sprintf(cards[i].value, "Jack");           cards[i].valueAmount[9] = 11;           score += 1;           break;                               case 'Q':           sprintf(cards[i].value, "Queen");           cards[i].valueAmount[10] = 12;           score += 1;           break;                                 case 'K':           sprintf(cards[i].value, "King");           cards[i].valueAmount[11] = 13;           score += 1;           break;                   case 'A':           sprintf(cards[i].value, "Ace");           cards[i].valueAmount[12] = 14;           score += 1;           break;```
Ive done as you said, so how would i implement this into sorting my values? :)
• 01-27-2013
c99tutorial
Quote:

Originally Posted by MF1991
Code:

```  typedef struct {   char* shortValue;   char shortSuit[5];   char value[20];   char suit[20];   char valueAmount[13];    } CARD; /*...*/  case '2':           sprintf(cards[i].value, "Two");           cards[i].valueAmount[0] = 2;           break;```
Ive done as you said, so how would i implement this into sorting my values? :)

I don't see how it could work this way. You need a single item (not an array) that can tell you what the value of a card is. For example, suppose you compare card N and card M. Which one comes first? I.e.. You need to be able to say something like

Code:

```if (card[N].valueAmount < card[M].valueAmount)     printf("card N is lower\n");```
In other words, get rid of the brackets on valueAmount
• 01-27-2013
MF1991
can you give me an example on how i can implement and place it within my code plz? :)
• 01-27-2013
c99tutorial
Quote:

Originally Posted by MF1991
can you give me an example on how i can implement and place it within my code plz? :)

Yes, get rid of the brackets and then use the if statement in the previous post
• 01-27-2013
MF1991
Code:

```  case '2':           sprintf(cards[i].value, "Two");           cards[i].valueAmount = 2;           break;                                   case '3':           sprintf(cards[i].value, "Three");           cards[i].valueAmount = 3;           break;                                   case '4':           sprintf(cards[i].value, "Four");           cards[i].valueAmount = 4;           break;                                 case '5':           sprintf(cards[i].value, "Five");           cards[i].valueAmount = 5;           break;                                 case '6':           sprintf(cards[i].value, "Six");           cards[i].valueAmount = 6;           break;                                 case '7':           sprintf(cards[i].value, "Seven");           cards[i].valueAmount = 7;           break;                                   case '8':           sprintf(cards[i].value, "Eight");           cards[i].valueAmount = 8;           break;                     case '9':           sprintf(cards[i].value, "Nine");           cards[i].valueAmount = 9;           break;                     case '0':           sprintf(cards[i].value, "Ten");           cards[i].valueAmount = 10;           break;                                 case 'J':           sprintf(cards[i].value, "Jack");           cards[i].valueAmount = 11;           score += 1;           break;                               case 'Q':           sprintf(cards[i].value, "Queen");           cards[i].valueAmount = 12;           score += 1;           break;                                 case 'K':           sprintf(cards[i].value, "King");           cards[i].valueAmount = 13;           score += 1;           break;                   case 'A':           sprintf(cards[i].value, "Ace");           cards[i].valueAmount = 14;           score += 1;           break;                       default:             printf("Invalid value character for card #%d. \n", i+1);             failed = 1;             break;         }     }```
Would i put the if statements under here and how many? or would it be under every case?
• 01-27-2013
c99tutorial
Quote:

Originally Posted by MF1991
Would i put the if statements under here and how many? or would it be under every case?

Put the if statements in your compar function. It should look like this:

Code:

```int compar(const void *a, const void *b) {         const CARD *carda = a;         const CARD *cardb = b; if (...)  // then carda comes before cardb   return -1; if (...)  // then carda comes after cardb   return 1; // otherwise carda and cardb are the same return 0; }```
Fill in the if statements so that the function returns -1 when a comes before b, 1 when a comes after b, and 0 when a and b are the same.

Since carda and cardb are pointers, you access the members with the -> operator rather than the . operator.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12