[Solved] How to Compare Character attribute?


I assume that you want to create a list of Pokemon elements and a table that states which element beats which.

As this is a highly irregular table, I recommend that you create it as an external file that is read in and then evaluated. Maybe begin with a list of the elements that induces the order and then a CSV-like block:

Water,Fire,Plant
-,>,<
<,-,>
>,<,-

You could also hard-code that as an array:

enum class Relation { stronger, weaker, same };
vector<vector<Relation> > relations = { { same, stronger, weaker},
                                        { ....

But hard-coding is a bad solution for something like that. Such a structure, like the enum class Relation and the vector relations is what you want to have after reading it, though. If you haven’t read in data so far, you might take a while to do that, but it would be a worthwhile exercise.

The enum is something I really recommend you to use, as it states things clear within the code, a stronger says more than a bool would, or even worse, an integer (If a relationship is described as a 3, you’d need to look it up), plus you can extend it (like slightly stronger and far stronger) easily.

Note that especially readers for CSV data already exist, you could use one of those. But again, nice exercise for the beginning and not that hard, there are tutorials for how to parse files like How can I read and parse CSV files in C++?.

General interface would be like:

enum class Relation { stronger, weaker, same };

class Relations
{
public:
    Relations(const string& file_name);
    Relation relationship(const string& element_1, const string& element_2) const;

private:
    vector<vector<Relation> > relations;
    vector<string> elements; // << stores the names of the elements and gives meaning to the entries of relations
};

A more sophisticated storing structure would be a map<string, map<string, Relation> > relations with the strings storing the names of the elements, allowing you to access it like Relation relation = relations["Fire"]["Water"];, but that might be overkill for now, assuming that you are a beginner. The advantage is that the structure would not require an order of elements from outside to make sense, the elements would be stored within, which is more organical in data representation.

I went somewhat far out with this answer, more or less giving you advice rather than answering your vaguely stated question, feel free to ask for more details. But in the end, if you are a beginner, note that SO might not be the right platform for this. A general forum about programming might be better, with you asking several questions after each other within a thread.

solved How to Compare Character attribute?