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

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 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. – Dai Jan 25 '20 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. – tadman Jan 25 '20 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. – Dai Jan 25 '20 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`. – tadman Jan 25 '20 at 23:43
  • You should learn C++ from a book which will teach you how to use "if" statements – Asteroids With Wings Jan 25 '20 at 23:43
  • @tadman It's unfortunate that the STL has a brick-wall learning curve. I wish C++ came with an "STL-Lite" that didn't side-track beginners by hassling over allocators and contiguous memory regions, argh! (Or just give-up and learn Java instead) – Dai Jan 25 '20 at 23:44
  • The way the logical operators work in C++ can be a bit perplexing at first, but if you have a good introduction or reference book to work from you can figure it out. Don't think C++ is something you can pick up by just having at it, poking and experimenting. You'll need a more formal approach due to how bizarre and non-sensical the syntax can be if you're not familiar with the C++ specification. – tadman Jan 25 '20 at 23:46
  • @Dai The Standard Library is fantastic and with the right introduction is actually pretty easy to use, vastly better than going at it with C++ "fancy C" style. While there's a lot more sharp edges in C++ than in Java, it can be made a lot more comprehensible than most introductory courses approach it. – tadman Jan 25 '20 at 23:47
  • What about using regular expressions (like `/dirt ?balls/i` in Perl)? – U. Windl Jan 26 '20 at 00:05

3 Answers3

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.

Dai
  • 141,631
  • 28
  • 261
  • 374
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!

Ashley
  • 897
  • 1
  • 5
  • 17
  • 2
    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. – Dai Jan 25 '20 at 23:41
  • Oh thanks! It's been a while since I've used C++ and clearly had forgotten. – Ashley Jan 26 '20 at 14:04
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.

Daniel
  • 8,179
  • 6
  • 31
  • 56
  • Usage of `` looks cool but the hacky implementation distracts from the answer itself. I suggest simply using `includes` function. – Burak Jun 21 '23 at 14:40