Inputting the name of the variable to display(total n00b)

Hi there! I'm very new to programming so I'm sorry to ask what is probably a very simple question, but I have asked friends and tried to look it up online and I cannot find the answer to my question.

I am writing a program to help manage the pathfinder campaign I am the GM of. I want to be able to do a variety of things using stats the I have assigned to a character. I created a struct "character" that contains 22 variables that all represent a stat of a character.

the struct looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct character{
int maxHP, str, dex, con, wis, intel, CR, XP, init, perception,
EAC, KAC, reflex, will, speed, cha, fort, stamina, resolve;


string name, melee, ranged;

}smWhelp, rich, soloman, scout, autpwn, herald;

character herald;
    herald.init= 3;
    herald.xp = 0;
    herald.stamina = 5;
    herald.resolve = 5;
    herald.maxhp = 9;
    herald.EAC = 13;
    herald.KAC= 14;
    herald.fort= 0;
    herald.reflex= 3;
    herald.will= 1;
    herald.speed= 30;
    herald.melee=
    herald.str= -2;
    herald.dex=3;
    herald.con=0;
    herald.cha=1;
    herald.intel=4;
    herald.wis=-1;


I am using a struct because I want to be able to, for example, pass two character stat lists into a function that would handle combat between the two characters.

What I find my self repeatedly needing to do is have the user input the name of a variable and have the console print the value for that variable.

For example, I want the following function to look up a stat from a character struct that matches the user's input.
1
2
3
4
5
6
7
string charName, charStat;
                cout << "Which character?" << endl;
                cin >> charName;
                cout << "What stat?" << endl;
                cin >> charStat;
                cout << charName.charStat << endl;


obviously charName.charStat doesn't work to call the stat the user input. how can I have the user inputs tell the program what variable to display?
Last edited on
You can just translate strings to variables something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

struct Character {
    std::string name;
    int init, xp, stamina;

    Character(const std::string& name,
              int init, int xp, int stamina)
        : name(name), init(init), xp(xp), stamina(stamina) {}

    int get(const std::string& s) const {
        if (s == "init")    return init;
        if (s == "xp")      return xp;
        if (s == "stamina") return stamina;
        return -1;
    }
};

int main() {
    Character herald("Bob", 3, 4, 5);
    std::cout << herald.get("xp") << '\n';
}

Alternatively you could use a map to hold the values, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
#include <initializer_list>
#include <map>

struct Character {
    std::string name;
    std::map<std::string, int> stats;

    Character(const std::string& name,
              std::initializer_list<std::pair<std::string,int>> t)
        : name(name)
    {
        for (auto& p: t) stats[p.first] = p.second;
    }

    int get(const std::string& s) {
        auto it = stats.find(s);
        return it == stats.end() ? -1 : it->second;
    }
};

int main() {
    Character herald (
        "Bob",
        { { "init",    3 },
          { "xp",      0 },
          { "stamina", 5 },
          { "resolve", 5 } });
    
    std::cout << herald.get("stamina") << '\n';
}

That works. Thanks!
Registered users can post here. Sign in or register to post.