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:

#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:

user@debian$ ./RUN Second
Matrix[ atoi(Second) ]:  1
user@debian$

BTW, if I change line 12 to this…

cout<<Matrix[ argv[1) ]<<"\n";

…the error message is this…

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

Dani AI

Generated

Good question, — the core issue is turning the argv string into an enum index rather than trying to treat the literal text as an integer (as pointed out). 's map suggestion is solid when you have many names or need fast lookups. A lighter-weight, easy-to-maintain alternative is to keep a single ordered list of names that mirrors the enum order and find the matching index at runtime.

This keeps enum-to-name binding in one place (so they can't drift) and avoids the overhead of a tree/map unless you need it. Example pattern:

#include <algorithm>
#include <array>
#include <iostream>
#include <string>

enum NyNumbers { First, Second, Third, Fourth, LAST_VALUE };

const char* names[LAST_VALUE] = { "First", "Second", "Third", "Fourth" };

int main(int argc, char* argv[]) {
  if (argc < 2) { std::cerr << "Usage: RUN <Name>\n"; return 1; }
  std::string key = argv[1];
  auto it = std::find(std::begin(names), std::end(names), key);
  if (it == std::end(names)) { std::cerr << "Unknown name: " << key << "\n"; return 2; }
  size_t idx = std::distance(std::begin(names), it);
  int matrix[LAST_VALUE] = { 1, 3, 7, 12 };
  std::cout << matrix[idx] << "\n";
}

Practical tips:

  • Validate argc and print a clear usage string.
  • If you need case-insensitive matching, normalize key before searching.
  • Use std::unordered_map (or the std::map approach from ) if lookups must be O(1) and there are many keys.
  • Avoid atoi for non-numeric input; if you ever accept numeric strings, prefer std::stoi with try/catch for error detection.
  • For long enums, consider an X-macro or single source-of-truth to generate both the enum and the names array so they stay synchronized.

Recommended Answers

All 3 Replies

cout<<Matrix[ atoi(argv[1]) ]<<"\n";
something i want to say about this line.
argv[1] is a char* type and you converting this to an int. doesn't make sense, and it is not related to the enum you defined

I think you are going to have to make a map of string to enum.

std::map<std::string, NyNumbers> theMap;
theMap["First"] = First;
// etc...
std::map<std::string, NyNumbers>::iterator pos;
if ( (pos = theMap.find(argv[1])) != theMap.end()){
  // Found!!
  cout << Matrix[pos->second];
  // etc...
}

Thanks histrungalot, this is a really, really cool suggestion. I'll give it a whirl...

Many thanks! This is an awesome forum! -P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.