c++ - Vector comparison with string -
i have string vector of user-input data containing strings. need make sure program won't execute if strings different specified few. vector contains 4 fields , every has different condition:
- vector[0] can "1" or "0"
- vector[1] can "red" or "green
- vector[2] can "1", "2" or "3"
- vector[3] can "1" or "0"
i tried writing if every condition:
if(tokens[0]!="1" || tokens[0]!="0"){ decy = "error"; } else if(tokens[1]!="red" || tokens[1]!="green"){ decy = "error"; } else if(tokens[2]!="1" || tokens[2]!="2" || tokens[2]!="3"){ decy = "error"; } else if(tokens[3]!="1" || tokens[3]!="0"){ decy = "error"; } else{ switch(){} //working code } return decy;
it enters first if , returns error. tried if instead of else if doesn't work either. checked vector[i]
contents , returns correct strings. no " " @ end of etc. removing else , releasing switch makes program check first condition , ignore rest of it.
i'm doing terribly wrong, can't find answer on internet decided ask here.
the conditions invalid.
any distinct value can satisfy conditions.
you should use &&
instead of ||
.
for example:
if (tokens[0] != "1" || tokens[0] != "0") {
consider line. if tokens[0]
"1"
, valid input, not satisfy first condition, satisfy second. want throw error when value neither of valid possible inputs.
this means condition should be:
if (tokens[0] != "1" && tokens[0] != "0") {
same goes others.
Comments
Post a Comment