Using a string/enum to access int Matrix[X] ?

This is a discussion on Using a string/enum to access int Matrix[X] ? within the C++ Programming forums, part of the General Programming Boards category; Hola code gurus, I’m wondering if there’s a way to use a string to access a specific item in a ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14

    Using a string/enum to access int Matrix[X] ?

    Hola code gurus,

    I’m wondering if there’s a way to use a string to access a specific item in a matrix of int[X].

    I have a program which uses enums as iterators to reference a large amount of data. To select an item in the matrix, the user will enter a string, which is also an enum, which also must serve as an iterator for something in the matrix. Here is a toybox example:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    enum NyNumbers { First, Second, Third, Forth, LAST_VALUE };
    
    int main(int argc, char* argv[]) {
    
      int Matrix[LAST_VALUE] = { 1, 3, 7, 12 };
    
      cout<<"Matrix[ atoi("<<argv[1]<<") ]:  ";
      cout<<Matrix[ atoi(argv[1]) ]<<"\n";
    
      return 0;
    }
    The idea is the user executes the program by typing “./RUN First” to print out the first element in the MyNumbers array, “./RUN Second” to access the second, and so on. I can’t use static numbers for the iterator. (i.e., “./RUN 1”) I must reference by enum/string.

    When run, the output of this problem is thus:

    Code:
    user@debian$ ./RUN Second
    Matrix[ atoi(Second) ]:  1
    user@debian$
    BTW, if I change line 12 to this…

    Code:
    cout<<Matrix[ argv[1] ]<<"\n";
    …the error message is this…

    Code:
    user@debian$ make
    g++ -g -Wall -ansi -pg -D_DEBUG_   Main.cpp -o exe
    Main.cpp: In function `int main(int, char**)':
    Main.cpp:12: error: invalid types `int[4][char*]' for array subscript
    make: *** [exe] Error 1
    user@debian$
    …which isn’t unexpected. Any idea how to imput the enum as argv[1]?

    Many thanks!
    -P
    Last edited by phummon; 04-02-2012 at 03:35 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to convert between the input string and the enum value name.
    Code:
    NyNumbers n;
    if (strcmp(argv[1], "First" == 0)
        n = First;
    else if (strcmp(argv[1], "Second") == 0)
        n = Second;
    //etc.
    else
        // error: argv[1] does not match any enum name
    If you have a large number of these, you might consider a map.
    Better yet, why not have the user simply enter in a number?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching word(string) in matrix.
    By Aleremilcapo in forum C Programming
    Replies: 7
    Last Post: 09-22-2010, 10:41 PM
  2. Convert enum member variable to string?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-10-2010, 03:59 PM
  3. Convert String to enum type
    By c9dw2rm8 in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 07:00 PM
  4. Matrix element access
    By nepper271 in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2008, 09:08 AM
  5. how to access dyn. string-arrays?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 11-27-2001, 10:37 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21