Dismiss
InnovationQ will be updated on Sunday, Oct. 22, from 10am ET - noon. You may experience brief service interruptions during that time.
Browse Prior Art Database

Switch Decisions Based on String Input in C

IP.com Disclosure Number: IPCOM000116115D
Original Publication Date: 1995-Aug-01
Included in the Prior Art Database: 2005-Mar-30
Document File: 2 page(s) / 32K

Publishing Venue

IBM

Related People

Nixon, T: AUTHOR

Abstract

In many applications, a program needs to react to one of several predefined words, phrases or strings that the user enters. Programs typically using string compares to check for an entered string and react to it. It is better programming practice to use a construct such as SWITCH, or CASE (depending on the implementation language) for handling "one of several" type decisions, but these operators require an enumerated data type, and not a string, as an argument.

This text was extracted from an ASCII text file.
This is the abbreviated version, containing approximately 100% of the total text.

Switch Decisions Based on String Input in C

      In many applications, a program needs to react to one of
several predefined words, phrases or strings that the user enters.
Programs typically using string compares to check for an entered
string  and react to it.  It is better programming practice to use a
construct such as SWITCH, or CASE (depending on the implementation
language) for handling "one of several" type decisions, but these
operators require an enumerated data type, and not a string, as an
argument.

      The solution is to provide a template for a routine that
converts from a string to an enumerated type defined by the template.
The routine converts from a string (i.e., the word EDIT or VIEW) to
an enumerated type (i.e., internal value 1 or 2).  This conversion is
done in only one place so that code updates (such as changing the
word
EDIT to CHANGE) become easier since there is one routine that
requires
modification.  A new word or phrase is added to the data type, by the
programmer by performing only two steps: add the new value to the
type
definition, and add one line to the conversion routine that contains
the input string and the output data type value.  The solution was
originally implemented in the 'C' programming language, however, a
similar solution could be written in other languages.