1
Diff FBullAndCow::EDifficulty(std::string diff) const
{
    if ((diff.length() > 1))
    {
        return Diff::Not_Number;
    }
    else if (!strchr(diff.c_str(), '3' || '4' || '5' || '6' || '7' || '8'))
    {
        return Diff::Not_Number;
    }
    return Diff::Ok;
}

Is it possible to find numerous characters in a string with strchr? I tried the method above, but it's not working. I suppose it's because strchr returns the occurrences of a character?

P.S:. I tried

    if ((!strchr(diff.c_str(), '3')) || (!strchr(diff.c_str(), '4')))

to use it this way too, though it was probably stupid. I'm a total rookie... I did try to look for a way for hours, but since I couldn't find anything, I'm here.

EDIT: It needs to return the number it finds. Sorry for leaving this out.

CorpseDead
  • 113
  • 2
  • 7
  • Optionally, I can write six different **if** statements, which is working, but that's rather ugly... – CorpseDead Sep 21 '17 at 13:44
  • What is `FString`? What is `Diff`? Any reason in particular you skipped `0`, `1`, `2`, and `9`? – Barry Sep 21 '17 at 14:33
  • @Barry FString is>> using FString = std::string; and Diff is an enum class. But that doesn't really matter here. Also, no particular reason why I skipped the rest of the numbers. Just examples. – CorpseDead Sep 21 '17 at 15:05
  • 1
    Don't use strchr, look at std::string::find_first_of, std::string::find_first_not_of – n. m. could be an AI Sep 21 '17 at 15:14
  • @CorpseDead Of course it matters. We know what a `std::string` is an can propose solutions based on that. We don't know what `FString` is. – Barry Sep 21 '17 at 15:46
  • 1
    Note that `’3’ || ‘4’` etc. has the value 1. `||` is a boolean operator; its result is `false` if both operands are `false` and `true` otherwise. – Pete Becker Sep 21 '17 at 16:37
  • If you want to do it in C, the function you’re looking for is `std::strpbrk`: `if (!std::strpbrk(diff.c_str(), “345678”))`. But, as several others have said, you’ve got a `std::string` coming in, and it has member functions to do this test. – Pete Becker Sep 21 '17 at 16:40
  • Describe what functionality you are trying to do, for me it looks like there are better ways to tackle this issue. This is called [XY problem](http://xyproblem.info/). – Marek R Sep 21 '17 at 16:59

1 Answers1

2

The direct answer is: no, you cannot check for multiple chars in strchr. That function just looks for one, specific character.

If you need to search for all the numeric characters, since you're using a std::string (why are you aliasing this?), you can use find_first_of(). Or, more likely, find_first_not_of(), checking that diff.find_first_not_of("0123456789") == std::string::npos.

However, even that's not a good solution - since presumably once you verify that it is numeric, you'll want the actual number. So it may be more direct to just use std::stoi() and verify that it didn't throw and consumed the whole string.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • After checking this function, it seems to do what I need, though I didn't know about it. Thanks a bunch! Let's say that I need an int input for example from two to nine. How would I use it in an __if__ statement? **diff** would be a string and I'd need to check if it is one of the numbers from 2 to 9. I know how to check its length. And the rest is? I guess I need a variant like: `std::string num = "23456789"; if () { return Diff::Ok; //this is an enum value. } ` Let's say I input 5 in the diff variant. How do I test if the input in diff is one of those numbers? @Barry – CorpseDead Sep 21 '17 at 16:44
  • @CorpseDead Just convert it to an `int`. – Barry Sep 21 '17 at 17:21