Hi all, I checked out a bit of posts similar to this one but none were quite what I am trying to do. I think the answer is that I cannot do it this way but if I do not ask I will not know for certain.
I am trying to compare a strings value with a variables name. My goal is just to prevent using a massive if..else statement every time. So for example:
int main (){
string stat;
int strength = 18;
int constitution = 18;
int dexterity = 18;
cout << "Which stat do you want to alter: Strength, Constitution or Dexterity?";
cin >> stat;
/*what I want to do - my guess is that if the variable names do not exist then there is nothing to compare to */
stat value = stat value + 2;
//what i do not want to do because duh
if (stat == "strength")
{ strength = strength + 2;}
elseif (stat == "constitution")
{ constitution = constitution + 2;}
else {dexterity = dexterity + 2;}
return 0;
}
If the variables are all of the same type, use a look up table which holds wrapped references.
It would be easier for the user to enter a choice if the key is an integer rather than a string.