0

Let's say I have some constants defined:

const int NUMBER = 5;
const int NUMBER_2 = 6;

Is there any way I can have the user input something like NUMBER or NUMBER_2 and convert that to the value without having to cin using a string and a lot of if statements or a switch statement?

I have a lot of constants defined this way, in this assignment, and I'm not supposed to change them (it would take way too long anyway).

Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
hhh
  • 111
  • 4
  • "The user input something like NUMBER or NUMBER_2 and convert that to the value" .....NUMBER is already 5, why would you have to convert it? – dinotom May 14 '16 at 01:59
  • 3
    No, this is not generally possible. Also note that in C++, literal `const` don't typically exist in a single memory location after compilation, they're inlined in the places where they're used, which eliminates some potential tricks you could do with variables. – Dai May 14 '16 at 02:01
  • 1
    Possible duplicate of [generic way to print out variable name in c++](http://stackoverflow.com/questions/6622962/generic-way-to-print-out-variable-name-in-c) – Luis Teijon May 14 '16 at 02:03
  • I agree that it's not generally possible. However it is possible to automate this using a script that's integrated into the application's build system; then keep a single list of values and their "labels" (for a lack of a better word), then use a script to robo-generate a bunch of spaghetti code that consists of all the declared values, their labels, and the lookup code to map between the two. – Sam Varshavchik May 14 '16 at 02:04
  • So is there an easier way to do this other than having the user input a string, which would be either "NUMBER" or "NUMBER_2", and then doing a lot of if statements? (e.g. if (str == "NUMBER") x = NUMBER;) I mean a lot of if statements because of my code having a lot of constants like that. – hhh May 14 '16 at 02:07
  • 2
    Sure, store your constants in a `std::map` and simply look them up with `find`. But they they aren't strictly constants. Constants are the wrong tool for this problem IMO. – Dark Falcon May 14 '16 at 02:10
  • You could store all the `const`s in an array and then you could display a menu to the user with options corresponding to indexes in the array. – Ahmed Akhtar May 14 '16 at 02:11
  • 1
    Possible duplicate of [C++: Use input string as variable name](http://stackoverflow.com/questions/23117238/c-use-input-string-as-variable-name) – Raymond Chen May 14 '16 at 02:43
  • I believe as well that you will most likely solve your problem with std::map. Still, to answer your question, you could do something pretty tricky and use the debugging symbols generated by your compiler to create a dictionnary of string that refer to address locations and then access the variable by its address. It is feasible, but most likely not what you need. – Pier-Yves Lessard May 14 '16 at 02:54
  • Note that writing constants in UPPERCASE is antipattern in C++ – Slava May 14 '16 at 04:34

0 Answers0