-1

To starts off, I am pretty new to C++. I am working on a project just for fun but keep running into a problem (see code below), no matter what my input word is, the code assigns the statRollArray value to the wisdom int. I have tried all of the different input methods, but it always acts as if I put in "wisdom". Why is this? Any help is appreciated!

while (charisma == 0 || strength == 0 || dexterity == 0 || intelligence == 0 || wisdom == 0 || consitution == 0) {

    cout << "Where would you like to assign your roll of " << statRollArray[chooseStatNumber] << "?" << endl;
    if (dexterity == 0) {
        cout << "Dexterity";
    }
    if (intelligence == 0) {
        cout << " Intelligence";
    }
    if (strength == 0) {
        cout << " Strength";
    }
    if (wisdom == 0) {
        cout << " Wisdom";
    };
    if (charisma == 0) {
        cout << " Charisma";
    }
    if (consitution == 0) {
        cout << " Constitution"
             << endl;
    }
    string choice = "";
    cin >> choice;
    if (choice == "wisdom" || "Wisdom" || "Dexterity" || "dexterity" || "Strength" || "strength" || "Intelligence" || "intelligence"
        || "Charisma" || "charisma" || "Constitution" || "constitution") {

        if (choice == "wisdom" || "Wisdom") {
            if (wisdom == 0) {
                wisdom = statRollArray[chooseStatNumber];
                chooseStatNumber += 1;
                continue;
            }
        }
        else {
            cout << "Wisdom already has been assigned!" << endl;
            continue;
        }
        if (choice == "Dexterity" || "dexterity") {
            if (dexterity == 0) {
                dexterity = statRollArray[chooseStatNumber];
                chooseStatNumber += 1;
                continue;
            }
        }
        else {
            cout << "Dexterity already has been assigned!" << endl;
            continue;
        }

        if (choice == "strength" || "Strength") {
            if (strength == 0) {
                strength = statRollArray[chooseStatNumber];
                chooseStatNumber += 1;
                continue;
            }
        }
        else {
            cout << "Strength already has been assigned!" << endl;
            continue;
        }

        if (choice == "intelligence" || "Intelligence") {
            if (intelligence == 0) {
                intelligence = statRollArray[chooseStatNumber];
                chooseStatNumber += 1;
                continue;
            }
        }
        else {
            cout << "Intelligence already has been assigned!" << endl;
            continue;
        }

        if (choice == "charisma" || "Charisma") {
            if (charisma == 0) {
                charisma = statRollArray[chooseStatNumber];
                chooseStatNumber += 1;
                continue;
            }
        }
        else {
            cout << "Charisma already has been assigned!" << endl;
            continue;
        }
        if (choice == "constitution" || "Constitution") {
            if (consitution == 0) {
                consitution = statRollArray[chooseStatNumber];
                chooseStatNumber += 1;
                continue;
            }
        }
        else {
            cout << "Constitution already has been assigned!" << endl;
            continue;
        }
    }
    else {

        cout << "Please choose one of the listed stats." << endl;
        continue;
    }
}
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52

1 Answers1

1

Why?

The conditionals expression is not evaluate as you think. For example:

if (choice == "wisdom" || "Wisdom" || "Dexterity" || "dexterity" || "Strength" || "strength" || "Intelligence" || "intelligence"
    || "Charisma" || "charisma" || "Constitution" || "constitution") 

first evaluates choice == "wisdom". If it's true the if clause is executed. But if it's false, the next || term is evaluated, i.e. "Wisdom" (not choice== "Wisdom"), which is not null and hence is considered true. So your condition will always be true.

You must be explicit on what you want to compare:

if (choice == "wisdom" || choice == "Wisdom" || choice == "Dexterity" ...)  

Isn't there a simpler way?

A simpler approach would be to pack the long list into a container, for example a set and use a simple lookup:

set<string> possible_terms{"wisdom", "Wisdom", "Dexterity",... }; 
if (possible_terms.count(choice))
    ... // found 
else ... // not found

Another simplification, would be to lowercase the input and make all comparisons with lowercase words. This would be more robust and avoids that you have to foresee every possible case-combination in your code:

for (auto&c:choice) 
    c = tolower(c); 
...
if (choice == "wisdom" ) ... // works with "wisdom", "Wisdom", "WISDOM", "wiSDom" etc... 
Christophe
  • 68,716
  • 7
  • 72
  • 138