#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    5

    C Shell Programming question


    How do I take in a string input from a user in C shell and then use a case/switch statement on that string?

    I'm trying to assign a numerical value to a three letter string from the user that's supposed to represent the abbreviation of a month so jan->1, feb->2,etc.
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,113
    Rep Power
    1803
    What is "C Shell"? Do you mean stdio? Either way it is probably not relevant to the answer.

    Code:
    int MonthNum( const char* month )
    {
        int num = 0 ;
        static const char*[] month_strings = { "", "Jan", "Feb", ... } ; // Complete this table
    
        int s = 1 ;
        while( num == 0 && s <= 12 )
        {
            if( strcmp( month_strings[i], month ) == 0 )
            {
                num = i ;
            }
        }
    
        return num ;
    }
    If rather than numbers 1 to 12 you just need a unique number for each month so you can perform a switch rather than a string compare, then since your strings are all three characters, and assuming they are 8 bit characters not wide characters, then you can pack the characters into a single 32 bit integer:

    Code:
    #include <stdint.h>
    #define CONST_MONTH_INT(m) (((#m)[2]<<16)|((#m)[1]<<8)|((#m)[0])))
    ...
    
    uint32_t month_num = (month_string[2] << 16) | (month_string[1] << 8) | month_string[0] ;
    
    switch( month_num )
    {
        case  CONST_MONTH_INT(Jan) :
        break ;
    
        case  CONST_MONTH_INT(Feb) :
        break ;
    
        case  CONST_MONTH_INT(Mar) :
        break ;
     
        ...
    }
  4. #3
  5. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    Clifford: I believe that the OP is referring to the C Shell, one of the more commonly used shells in Unix.

    Astrodude: Despite the name, C shell doesn't have much to do with the C language or it's relatives. Questions about scripting languages should probably be posted to either 'Other Languages' or 'Scripts', though the latter is mostly about web scripting rather than shell scripting. Perhaps 'Unix Help' or 'Linux Help' would be appropriate, for that matter.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    5
    Originally Posted by Schol-R-LEA
    Clifford: I believe that the OP is referring to the C Shell, one of the more commonly used shells in Unix.

    Astrodude: Despite the name, C shell doesn't have much to do with the C language or it's relatives. Questions about scripting languages should probably be posted to either 'Other Languages' or 'Scripts', though the latter is mostly about web scripting rather than shell scripting. Perhaps 'Unix Help' or 'Linux Help' would be appropriate, for that matter.
    Thanks,
    will do.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    I decided to not learn the nuances of all available major shells. This bash script passed 2 tests I tried:
    Code:
    #!/bin/bash
    
    # script sets the shell variable name
    
    # $ . b.sh     # example
    # 1
    # January
    # $ echo $name
    # January
    # $ 
    
    #How do I take in a string input from a user in C shell and then use a case/switch statement on that string?
    
    #I'm trying to assign a numerical value to a three letter string from the user that's supposed to represent the abbreviation of a month so jan->1, feb->2,etc.
    
    read number
    name=ERROR
    case $number in
    (1) name=January ;;
    (2) name=Feb ;;
    (*) echo ERROR! > /dev/stderr
    esac
    echo $name
    Last edited by b49P23TIvg; November 1st, 2012 at 07:50 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo