Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



C Programming Language (2nd Edition)    (ISBN: 0131103628)


 

 List Price: $40.00
 Our Price: $40.00
 Used Price: $24.95

 Release Date: 22 March, 1988
 Manufacturer: Prentice Hall PTR (Paperback)
 Sales Rank: 2,265

 Author: Brian W. Kernighan, Dennis Ritchie, Dennis M. Ritchie









More Info

 Case 2001-11-25 03:47:46
 
Category: source:c:runtime
Description: Example of using switch() and case to filter out the args given to main()
Platform: unix
Author: detour
Viewed: 1638
Rating: 0/5 (0 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
case.c
/* case.c written by detour@metalshell.com
 *
 * Example of using switch() and case to filter out the args
 * given to main()
 *
 * http://www.metalshell.com/
 *
 */

#include <stdio.h>

void pHelp(char *me);

int main(int argc, char *argv[]) {
  int x;

  /* If they gave no arguments */
  if(argc < 2) {
    pHelp(argv[0]);
    exit(1);
  } 

  for(x=1;x<argc;x++) {

    if(argv[x][0] == '-') {

      switch( argv[x][1] ) {
        case 'h':
        {
          pHelp(argv[0]);
	  break;
        }
        case 'a':
        {
          printf("Hello\n");
          break;
        }
        case 'b':
        {
          printf("Goodbye\n");
          break;
        }
        /* Catches anything not picked up by case */
        default:
          printf("Unknown Option %s\n", argv[x]);
      }

    }

  }
}

/* Help Screen */
void pHelp(char *me) {
    printf("Usage %s [OPTION]\n\n", me);
    printf("-h,\t\tprints this screen\n");
    printf("-a,\t\tprints HELLO and exits\n");
    printf("-b,\t\tprints GOODBYE and exits\n"); 
}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?