How do I use OR with if statements in regards to strings

Etienne Andlau

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 :-)

Dai

Many programming languages today (C, C++, Swift, C#, Java, JavaScript, PHP) share C's curly-brace syntax and operator syntax.

  • The syntax for a short-circuited logical OR is a double-pipe: ||.
  • The syntax for a non-short-circuited logical OR is a single-pipe: | (this is also used for bitwise OR operations).
  • Also, use && 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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I reset everything in regards to my network connection?

분류에서Dev

How do I change the text of an output using if statements in jQuery?

분류에서Dev

How do I consolidate case when statements that have (SUM)?

분류에서Dev

How do I consolidate case when statements that have (SUM)?

분류에서Dev

How do i save strings to an array and display the strings in an label?

분류에서Dev

How do I store an array of strings into another array of strings?

분류에서Dev

How do I use MapStateListener?

분류에서Dev

How do I convert a list of strings to a unicode value?

분류에서Dev

How do I slice strings in Google Apps Script like in Python?

분류에서Dev

How do I replace a comma separated list of strings with the first occurrence?

분류에서Dev

How do i use Blazor components in the code?

분류에서Dev

Python: How do i use itertools?

분류에서Dev

How do I use runit with zookeeper

분류에서Dev

How do I use Firefox cookies with Wget?

분류에서Dev

How do I use the GeometryConstraint class?

분류에서Dev

How do I make use of rotation matrices?

분류에서Dev

How do I configure ActiveJob to use Resque?

분류에서Dev

How do i use SetPixel on a new Bitmap?

분류에서Dev

How do i get cURL to use https

분류에서Dev

How do I use "<<" with my own struct?

분류에서Dev

How do I use URL directories as variables?

분류에서Dev

What is fileAPI ? How do I use it with angular?

분류에서Dev

How do I open and use phpmyadmin?

분류에서Dev

How do I configure Docker to use ZFS?

분류에서Dev

How do I use the latest GCC on Ubuntu?

분류에서Dev

How do I use man pages to learn how to use commands?

분류에서Dev

How do I install and use PyCharm without having to use a terminal?

분류에서Dev

How do I install and use PyCharm without having to use a terminal?

분류에서Dev

Why i get unsuspected behavior when I use the if/else statements?

Related 관련 기사

  1. 1

    How do I reset everything in regards to my network connection?

  2. 2

    How do I change the text of an output using if statements in jQuery?

  3. 3

    How do I consolidate case when statements that have (SUM)?

  4. 4

    How do I consolidate case when statements that have (SUM)?

  5. 5

    How do i save strings to an array and display the strings in an label?

  6. 6

    How do I store an array of strings into another array of strings?

  7. 7

    How do I use MapStateListener?

  8. 8

    How do I convert a list of strings to a unicode value?

  9. 9

    How do I slice strings in Google Apps Script like in Python?

  10. 10

    How do I replace a comma separated list of strings with the first occurrence?

  11. 11

    How do i use Blazor components in the code?

  12. 12

    Python: How do i use itertools?

  13. 13

    How do I use runit with zookeeper

  14. 14

    How do I use Firefox cookies with Wget?

  15. 15

    How do I use the GeometryConstraint class?

  16. 16

    How do I make use of rotation matrices?

  17. 17

    How do I configure ActiveJob to use Resque?

  18. 18

    How do i use SetPixel on a new Bitmap?

  19. 19

    How do i get cURL to use https

  20. 20

    How do I use "<<" with my own struct?

  21. 21

    How do I use URL directories as variables?

  22. 22

    What is fileAPI ? How do I use it with angular?

  23. 23

    How do I open and use phpmyadmin?

  24. 24

    How do I configure Docker to use ZFS?

  25. 25

    How do I use the latest GCC on Ubuntu?

  26. 26

    How do I use man pages to learn how to use commands?

  27. 27

    How do I install and use PyCharm without having to use a terminal?

  28. 28

    How do I install and use PyCharm without having to use a terminal?

  29. 29

    Why i get unsuspected behavior when I use the if/else statements?

뜨겁다태그

보관