Thread: C Beginning

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    C Beginning

    Hello, i am trying to make a comeback into C, i have the following doubts.

    - When the compiler compiles the program, will i be able to see the debug messages, that the compiler is using when it compiles a line.

    - char card_name[3] is a array, what is the significance of 3, is it the maximum characters in the array.

    - What is the significance of %2s ?

    - So i think you get the jist of the program. What if if i wanted to read the number 10. How can i generalize the program for all the 13 cards.

    Code:
    int main() 
    {
        char card_name[3];
        puts("Enter the card_name: ");
        scanf("%2s", card_name);
        int val= 0;
        
        if (card_name[0]=='K'){
            val=10;
        }else if (card_name[0]=='Q'){
            val=10;
        }else if (card_name[0]=='J'){
            val=10;
        }else if (card_name[0]=='A'){
            val=11;
        }else{
            val=atoi(card_name);
                }
        printf("The card value is: %i\n", val);
        
        return 0;
    }
    Thanks
    Bharat C P

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I'd use fgets. if val is 10 on k, q, or j, then you could use

    Code:
    if(toupper(card_name[0]) == 'K' || toupper(card_name[0]) == 'Q' || toupper(card_name[0]) == 'J')
        val = 10;
    else if ...
    else ...
    atoi returns 0 on failure, so you might try sscanf instead since it returns the number of successfully converted and stored items.

    Code:
    if((sscanf(card_name, "%d", &val)) == 1)
        // got the number
    else
       // some rubbish input
    the questions sound like HW, so I'll pass on those.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    Quote Originally Posted by ronin View Post
    I'd use fgets. if val is 10 on k, q, or j, then you could use

    Code:
    if(toupper(card_name[0]) == 'K' || toupper(card_name[0]) == 'Q' || toupper(card_name[0]) == 'J')
        val = 10;
    else if ...
    else ...
    atoi returns 0 on failure, so you might try sscanf instead since it returns the number of successfully converted and stored items.

    Code:
    if((sscanf(card_name, "%d", &val)) == 1)
        // got the number
    else
       // some rubbish input
    the questions sound like HW, so I'll pass on those.

    Ronin,

    Thanks for the reply, i will try to get that. Actually i checked the output, the element that confused me the card_name[0] i thought for 10 it will read the 1 but i guess i was wrong.

    C Beginning-555-jpg

    I guess, the else if are only for K,J,Q,A... .

    I am trying to start from scratch.

    Thanks
    Bharat C P

  4. #4
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by BharatNT2IE View Post
    Hello, i am trying to make a comeback into C, i have the following doubts.

    - When the compiler compiles the program, will i be able to see the debug messages, that the compiler is using when it compiles a line.
    The compiler outputs error-messages if needed, and stops without producing the executable. It can also output warnings & notifications, but usually you have to enable them first.

    If you are using any recent flavor of gcc, try adding -Wall -Wextra in the command-line options.

    - char card_name[3] is a array, what is the significance of 3, is it the maximum characters in the array.
    Yes. But since you are dealing with character arrays for treating them as c-strings via the standard library, the last character must be '\0'. That's why you need to define an extra space for it (in your case 3 instead of 2).

    - What is the significance of %2s ?
    It instructs scanf() to read only the first 2 characters.

    - So i think you get the jist of the program. What if if i wanted to read the number 10. How can i generalize the program for all the 13 cards.
    Generalizing would involve a more structural approach, assuming you want to avoid a long switch-statement or multiple if-else statements.

    Here's a simplified way of doing it, by checking your input against the contents of two pre-defined arrays: one holding card labels, and one mapping corresponding card values.

    Re-designing it using structs would be more scalable though...

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define NFACES    13
    
    char faceLabels[NFACES][3] = {
        "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"
    };
    int  faceValues[NFACES] = {
        0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 11
    };
    
    /* ----------------------------------------
     * Convert all characters of a cstring to upper-case
     */
    void s_toupper( char *s )
    {
        if ( s )
        {
            while ( *s ) {
                *s = toupper(*s);
                s++;
            }
        }
    }
    
    /* ----------------------------------------
     * Look for cstring label into the array of cstrings faceLabels.
     * Return the index of label inside faceLabels on success, -1 otherwise.
     */
    int faceLabel_lookup( const char label[3], char faceLabels[NFACES][3] )
    {
        int i;
    
        /* sanity checks */
        if ( !label || !faceLabels )
            return -1;
    
        for (i=0; i < NFACES; i++)
            if ( 0 == strcmp(label, faceLabels[i]) )
                return i;
    
        return -1;
    }
    /* ---------------------------------------- */
    int main( void )
    {
        char label[3] = {'\0'};
        int faceIndex = -1;
    
        puts("Enter label of the card: ");
        scanf("%2s", label);
    
        s_toupper(label);
        faceIndex = faceLabel_lookup(label, faceLabels);
        if ( -1 == faceIndex ) {
            puts( "*** there's no such card" );
            return 1;
        }
        printf( "The card value is: %d\n", faceValues[faceIndex] );
    
        return 0;
    }
    Last edited by migf1; 05-22-2013 at 03:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginning C#
    By DennChooch in forum C# Programming
    Replies: 7
    Last Post: 11-21-2012, 05:52 AM
  2. Just Beginning
    By gamblingman in forum General Discussions
    Replies: 38
    Last Post: 10-11-2012, 01:19 AM
  3. Beginning ASM.
    By Krak in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 06:27 PM
  4. beginning C++
    By datainjector in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2002, 02:27 AM
  5. In the beginning...
    By CAP in forum Game Programming
    Replies: 21
    Last Post: 05-29-2002, 01:40 PM