Parsing the name of constants from a file

So, let's say that I have
1
2
const int INTEGER_NUMBER = 4;
int x;

and I'm parsing in a text file, and retrieve the string "INTEGER_NUMBER" so that I have
 
char *st = "INTEGER_NUMBER";

what do I do next so that I can assign
 
x = INTEGER_NUMBER;

?
1
2
3
4
if (strcmp(st, "INTEGER_NUMBER") == 0)
{
	x = INTEGER_NUMBER;
}
Whoops, should have been more clear. I don't want to actually have the line

 
x = INTEGER_NUMBER;


The string could be anything from "INTEGER_NUMBER" to "POTATO_PANCAKE", and I want to be able to assign the value based on whatever is parsed from the file. So it would be something like

 
x = //whatever constant name is held in st 
You'd need some sort of associative container. C++ doesn't know the names you give your variables.
Try std::map:
http://www.cplusplus.com/reference/map/map/
Or, slightly faster (although string hashing is generally slow), unordered map:
http://www.cplusplus.com/reference/unordered_map/unordered_map/
Last edited on
Ah, thank you! Totally forgot about maps. This is basically what they were made for.
Topic archived. No new replies allowed.