Thread: Sorting this in C language.

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    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)

  2. #17
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    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";

  3. #18
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    "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...

  4. #19
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    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]

  5. #20
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    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.

  6. #21
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    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

  7. #22
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by MF1991 View Post
    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.

  8. #23
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    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?

  9. #24
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by MF1991 View Post
    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

  10. #25
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    can you give me an example on how i can implement and place it within my code plz?

  11. #26
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by MF1991 View Post
    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

  12. #27
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    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?

  13. #28
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by MF1991 View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 07-15-2012, 05:20 PM
  2. Is C++ or C language is a high Level language?
    By uthmankhale in forum C++ Programming
    Replies: 5
    Last Post: 08-25-2011, 06:00 PM
  3. What's the Difference Between a Programming Language and a Scripting Language?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-15-2005, 04:46 PM
  4. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  5. Computer Language VS Spoken Language
    By Isometric in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-04-2002, 03:47 PM