Ok, I know I'm probably programing in some kind of archaic way for C++ but my knowledge base is rather limited.
I'm having problems with my input not being identified correctly (or however you would say it, rusty is me)

cin>>StatsChange;
if (StatsChange == "Strength" or "strength" or "str" or "STR" or "Str")
{
//Strength Changes
Strength = Strength + 1;
abilpts = abilpts -1;
goto hpmpstat;
}
else if (StatsChange == "Dexterity" or "dexterity" or "dex" or "DEX" or "Dex")
{
//Dexterity Changes
Dexterity = Dexterity + 1;
abilpts = abilpts -1;
goto hpmpstat;
}
else if (StatsChange == "Constitution" or "constitution" or "con" or "CON" or "Con")
{
//Constitution Changes
Constitution = Constitution +1;
abilpts = abilpts - 1;
goto hpmpstat;
}
else if (StatsChange == "Intelligence" or "intelligence" or "int" or "INT" or "Int")
{
//Intelligence Changes
Intelligence = Intelligence + 1;
abilpts = abilpts -1;
goto hpmpstat;
}
else if (StatsChange == "Wisdom" or "wisdom" or "wis" or "WIS" or "Wis")
{
//Wisdom Changes
Wisdom = Wisdom + 1;
abilpts = abilpts -1;
goto hpmpstat;
}
else if (StatsChange == "Charisma" or "charisma" or "cha" or "CHA" or "Cha")
{
//Charisma Changes
Charisma = Charisma +1;
abilpts = abilpts -1;
goto hpmpstat;
}
else
{
//error statement
cout<<"You fail"<<endl;
system("PAUSE");
goto CCRT;
}

When You type in like Dex or something it still only applies the strength bonus. Also if you type in random letters it only goes to the "//strength changes" part of the code instead of the "//error statement" part of the code. Can someone help me resolve this issue? Or bring me up to speed with some code that could help me?

Recommended Answers

All 2 Replies

if (StatsChange == "Strength" or "strength" or "str" or "STR" or "Str")

should be:

if (StatsChange == "Strength" || StatsChange == "strength" || StatsChange== "str" || StatsChange== "STR" ||StatsChange== "Str")

I know, believe me I did the same thing when I first got into boolean logic.. because the way you do it (aside from the incorrect symbology) seems correct at first, and more code efficient. However, please realize that there are specific rules at play here; each expression is handled individually.

If only one variable exists, the boolean test will either return TRUE if not zero, and FALSE if zero. Example:

if(variable)
{
     //code 
}

In this instance, if 'variable' contains any value, it will test TRUE and will enter/execute the contents of the if() structure. Conversely, if 'variable' contains a zero, the boolean expression will test FALSE and will simply skip over the entire if() structure.

So like me, you are probably wondering how to avoid these huge multiple lines of logic. Here is one way I can think of:

string input = {"Strength", "strength", "str", "STR", "Str"};
bool match = FALSE:
int i=0;

do{
     if(StatsChange == input[i])
     {
          match = TRUE;
     }
}while(match == FALSE || i < 5);

Placing boolean tests inside of a loop could potentially eliminate several lines of logical testing.

Thanks so much for the help. IT works! Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.