Question about organization

Alright, is there a way to organize words into categories. Like classify "good", "well", "fine" as Positive. And "bad", "not good", "terrible" as Negative. I know you can do:

if (mystr == "good" || mystr == "well" || mystr == "fine")

but can I have:

if (mystr == Positive) and Positive includes all the words like "good", okay", "fine", etc. Can I do that? Thanks
You can make a vector of strings, and test mystr against it.
You can create a function.
1
2
3
4
bool isPositive(const std::string& str)
{
	return str == "good" || str == "well" || str == "fine";
}

then to test if mystr is "positive" you just do:
if (isPositive(mystr))
Last edited on
Peter87, would I have to do the bool thing at the beginning of the program where the strings are declared?
You need to declare the function before you can use it.

ProCoder's way is also good. You can combine both methods, putting the code that checks if the string is contained in the vector (or whatever container you choose to use) inside the function.
Last edited on
You could drop strings and try something like this (warning: I didn't compile it):

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
33
34
35
36
37
38
39
40
enum StatusType
{
    PositiveType,
    NeutralType,
    NegativeType
};

Class Status 
{
public:
    Status(StatusType t) : type(t) {}
    StatusType getType() { return type; }
private:
    StatusType type;
};


const Status fine(PositiveType);
const Status well(PositiveType);
const Status good(PositiveType;
const Status bad(NegativeType);

void evaluate(cosnt Status& stat)
{
    if (stat.getType() == PositiveType)
    {
        ...
    }
}

int main()
{
    Status myStatus = fine;
    evaluate(myStatus);

    myStatus = bad;
    evaluate(myStatus);

    evaluate(good);
}
Topic archived. No new replies allowed.