I want to do an if statement to compare two strings to see if they match. So:
if (TypedAnswerOne == CorrectAnswerOne)
The code works if the correct answer is typed EXACTLY as it is in the CorrectAnswerOne string value. No problems at all.
However ... if the answer is typed slightly different as one word in stead of two words for example then it shows that the answer is wrong.
So I was wondering how do I can I do an "OR" with strings?
So:
if (TypedAnswerOne == CorrectAnswerOne or "dirtballs" or "Dirt Balls" or "dirt balls")
How can I define "or" in CPP with strings?
TY :-)
Many programming languages today (C, C++, Swift, C#, Java, JavaScript, PHP) share C's curly-brace syntax and operator syntax.
||
.|
(this is also used for bitwise OR operations).&&
for a short-circuited logical AND and &
for non-short-circuited logical AND or bitwise AND.^
is XOR (and not to-the-power-of).C++, like these other languages, does not have a built-in feature to let you compare a single left-hand value with multiple right-hand values, so you need to repeat the left-hand value.
Like so:
if( TypedAnswerOne == "dirtballs" || TypedAnswerOne == "Dirt Balls" || TypedAnswerOne
== "dirt balls" )
C and C++ do not support strings in switch
statements unlike Java, C#, Swift and PHP, btw.
BTW, you should use a case-insensitive string comparison instead of defining all possible values yourself.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다