Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 30th, 2012, 10:37 PM
 Astrodude
Contributing User

Join Date: Oct 2012
Posts: 37
Time spent in forums: 7 h 37 m 25 sec
Reputation 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
October 31st, 2012, 04:45 AM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,969
Time spent in forums: 1 Month 4 Days 4 h 2 m 28 sec
Reputation Power: 1801
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 ;

...
}```

#3
October 31st, 2012, 08:55 AM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,799
Time spent in forums: 1 Month 2 Weeks 3 Days 2 h 1 m 23 sec
Reputation Power: 1569
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 ShortUnderstanding the C/C++ Preprocessor
Taming PythonA 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

#4
October 31st, 2012, 10:54 PM
 Astrodude
Contributing User

Join Date: Oct 2012
Posts: 37
Time spent in forums: 7 h 37 m 25 sec
Reputation Power: 5
Quote:
 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.

#5
November 1st, 2012, 08:48 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,184
Time spent in forums: 1 Month 3 Weeks 2 Days 12 h 45 m 36 sec
Reputation Power: 455
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.

name=ERROR
case \$number in
(1) name=January ;;
(2) name=Feb ;;
(*) echo ERROR! > /dev/stderr
esac
echo \$name```
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : November 1st, 2012 at 08:50 AM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > C Shell Programming question

## Developer Shed Advertisers and Affiliates

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |