开发者

Why wont my "intellect" variable add by 5?

开发者 https://www.devze.com 2023-02-11 00:19 出处:网络
if (checkForRoll == \"intellect\" && checkForRoll == \"Intellect\") {//checks for intellect intellect = intellect + 5;
if (checkForRoll == "intellect" && checkForRoll == "Intellect") {//checks for intellect
        intellect = intellect + 5;
    } else if (checkForRoll == "strength" && checkForRoll == "Strength") {
        strength = strength + 5;
    }
    cout << intellect;
开发者_如何学C

When I execute this, the intellect int does not add by 5. Why?


You are requiring your string to equal both intellect and Intellect which is impossible. Change the "and" (&&) to an "or" (||).


Your variable checkForRoll can't be 'strength' && 'Strength', it can be 'strength' || 'Strength' however.


CheckForRoll cannot be both "intellect" and "Intellect".

Assuming you are using a std::string convert to upppercase, and do one comparision.

std:string checkForRoll = "inTeLleCt";

std::transform(checkForRoll.begin() , checkForRoll.end() , checkForRoll.begin(), ::toupper);

if (checkForRoll == "INTELLECT")
{
....
}


This will only work of checkForRoll is a string
If it is a char* then you can't test for equality with ==. You need to use strcmp() to compare.


To make your life easier, I suggest you use std::toupper or std::tolower to convert the string before you compare. See also std::transform.

This simplifies your life by using only one comparison.

"Try it, you'll like it!"


I think you should replace && with ||

P.S. you could also write intellect += 5

0

精彩评论

暂无评论...
验证码 换一张
取 消