Thread: defined values lookup

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    36

    defined values lookup

    example pseudo code

    Code:
    #define red 7
    
    //read in command line argument 'red' into char*
    Is there anyway to have 'red' represent 7? The reason I need this is to index into an array from the users command line args and change the value to the next command line arg

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    enum Color {RED, BLUE, YELLOW};
    const char* color_table[] = {"red", "blue", "yellow"}
    
    unsigned short int input;
    
    cout << "<0> Red" << endl
         << "<1> Blue" << endl
         << "<2> Yellow" << endl
         << endl
         << "Enter your choice: ";
    cin >> input;
    if(input < 3)
        cout << "You entered " << color_table[input] << endl;
    Does that answer your question?

    color_table[RED] == "red" and so on.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you looking for the string "red" to represent 7, or an enumeration?
    Code:
    enum colors { Red = 7, Orange, Yellow, Green, Blue, Indigo, Violet };
    Or you could do something like:
    Code:
    struct color
    {
        char *name;
        int value;
    };
    
    ...
    
    struct color colorlist[] = 
    {
        { "red", 7 },
        { "orange", 3 },
        { "yellow", 27 },
        ...
    };
    You'll need to be more specific as to what you're looking for.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    36
    similar to the enum
    Code:
    enum colors {orange, yellow, red}
    So red is 3 in this case.

    Well in the array read in is like:

    Code:
    arr[red]=(some Object)
    so I can read in the word 'red' and then index into the proper position in the array to change the object as needed.

    The catch with creating an array like
    colarr[] = ("orange","yellow","red")
    is that there is roughly 1000 values in the enum for me (it isn't colors but a more complex application and I need to read in values from the command line to change the default Objects in that arr[])

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Write a program to write the code necessary for a const array containing the 1000 values.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I usually use the preprocessor:

    Code:
    // data.h
    ENTRY(red, 1)
    ENTRY(blue,2)
    ENTRY(green,3)
    
    // stuff.c
    #define ENTRY(s,i) s = i,
    enum colors
    {
    #include "data.h"
    };
    #undef ENTRY
    
    void add(HLOOKUPTABLE tbl, const char *name, int value);
    
    void initTable(HLOOKUPTABLE tbl)
    {
    #define ENTRY(s,i) add(tbl, #s, i);
    #include "data.h"
    #undef ENTRY
    }
    etc.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Boy, that's horrible to read.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It works. Mozilla uses that, just for example.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. VOID (my understanding is void)
    By LLINE in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2008, 04:10 PM
  2. Table of X & Y Values.
    By danlee58 in forum C Programming
    Replies: 5
    Last Post: 10-15-2008, 04:29 AM
  3. Nasty linker problem; multiply defined
    By cboard_member in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2005, 03:06 PM
  4. Struct Values
    By Muphin in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2005, 09:24 PM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 02:08 PM