0

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

9
  • Btw, C++ is almost never referred to as "CPP". You may see .cpp used as a file-extension but only because using plus symbols in filenames causes problems. Commented Jan 25, 2020 at 23:39
  • Tip: Make a container (e.g. std::vector) of your intended matches, then check that the string you want is in that list. If you're just matching against simple variants, maybe what you want is a regular expression that's more lenient, or you want to clean up the input, stripping spaces and lower-casing etc., to match more broadly. Commented Jan 25, 2020 at 23:40
  • 1
    @tadman If we're going to get the OP start using the STL, then this kind of comparison is best done with an std::unordered_set with a case-insensitive hashing function. Commented Jan 25, 2020 at 23:42
  • @dai C++ is honestly pretty terrible without the Standard Library, so learning it is essential. Good recommendation there about std::unordered_set. Commented Jan 25, 2020 at 23:43
  • You should learn C++ from a book which will teach you how to use "if" statements Commented Jan 25, 2020 at 23:43

3 Answers 3

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to define the OR between cases, not between strings. So, for example:

if (x == "dirtbag" || x == "dirt bag") 

You can have as many ORs as you want, but it starts to get messy. When that happens, you might prefer a switch case:

switch(x) { case "dirtbag" : ...; // do something case "dirt bag" : ...; // do something } 

If you're open to using pre-existing libraries and don't want to handle all of the cases (it sounds like there could be a lot!) you could always find one that suits your needs and maybe handles the string before comparing (setting it to lowercase, removing whitespace, etc). Good luck!

2 Comments

C++ does not support using strings in switch() statements. Only integer values can be used because switch statements are typically compiled down to a native jump-table. Languages like C# and Java compile string-switch statements to a runtime-generated Map or Dictionary lookup.
Oh thanks! It's been a while since I've used C++ and clearly had forgotten.
0

For more than a couple of possible values, I tend to put them in a container and use an algorithm:

#include <algorithm> #include <iterator> #include <array> #include <string> #include <iostream> template <typename T, std::size_t N, typename U> bool includes(const std::array<T, N>& arr, const U& value) { return std::find(std::cbegin(arr), std::cend(arr), value) != std::cend(arr); } struct in_tag {} in; template <typename U> struct in_op_temporary { const U& value; }; template <typename U> in_op_temporary<U> operator<(const U& lhs, in_tag rhs) { return {lhs}; } template <typename U, typename T, std::size_t N> bool operator>(in_op_temporary<U> lhs, const std::array<T, N> rhs) { return includes(rhs, lhs.value); } int main() { const std::array<std::string, 3> answers { "dirtballs", "Dirt Balls", "dirt balls" }; if ("Dirt Balls" <in> answers) std::cout << "success!" << std::endl; if (not ("DirtBalls" <in> answers)) std::cout << "success!" << std::endl; } 

Live On Coliru

But for this particular problem, I'd suggest finding a more general way of accounting for errors in the string.

1 Comment

Usage of <in> looks cool but the hacky implementation distracts from the answer itself. I suggest simply using includes function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.