-6

I'm getting a warning code C682 and im newer to programming so I'm not sure exactly what is wrong.

bool isOperator(char ch)
{

    if (ch == '(' or ')' or '*' or '-' or '+')
    {
        return true;
    }
    else 
        return false;
}

I want it to return true if the char is one of those and false if it's something else.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Dakota
  • 13
  • 4
  • 1
    return (ch == '(') || (ch== ')') || (ch == '*') || (ch == '-') || (ch == '+'); – Omid CompSCI Sep 12 '19 at 22:24
  • 5
    OK, an advice: *Don't try random things*. Why did you decide that this code would work? Which book/tutorial gave you that idea? Learn the basics (there are plenty of materials out there), and then write a code you are absolutely confident in (you'll find out that even then it often doesn't work). –  Sep 12 '19 at 22:25
  • Provide full error message in addition to code number. – Jarod42 Sep 12 '19 at 22:26
  • I know there is a duplicate of this question, but I can't find it. – Remy Lebeau Sep 12 '19 at 22:33
  • This should be of use: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Sep 12 '19 at 22:33

4 Answers4

3

or is a logical operator in C++ so you need to put conditional operators:

 return ch == '(' or ch == ')' or ch == '*' or ch == '-' or ch == '+');

otherwise you evaluate 'c' and others as an expression which is always true.

Some may see this more readabale (it is less repeatative):

bool isOperator(char ch)
{
    switch( ch ) {
       case '(' : 
       case ')' :
       case '*' :
       case '-' :
       case '+' :
           return true;
    }
    return false;
}

or more C++ way (though it is more expensive but in your case of 5 variants unlikely to matter):

 bool isOperator(char ch)
 {
    const std::string_view ops( "()*-+" );
    return ops.find( ch ) != std::string_view::npos;
 }
Slava
  • 43,454
  • 1
  • 47
  • 90
2

Alternatively, you could implement it as a search into an array. This way you can add and remove operators easily.

#include <algorithm>
#include <iterator>

bool isOperator(char op) {
  const char operators[] = {'(', ')', '+', '-', '*'};

  return std::find(std::begin(operators), std::end(operators), op) != std::end(operators);
}
Gerard097
  • 815
  • 4
  • 12
2

The right way to write it would be

if (ch == '(' or ch == ')' or ch == '*' or ch == '+')


In addition to the other answers here, you could use std::any_of:
bool isOperator(char ch)
{
  static const std::array arr { '(', ')', '*', '-', '+' };
  return std::any_of(arr.begin(), arr.end(), [ch](char const& c) {
    return ch == c;
  });
}
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
-1

Lets go over a few things: Replace or with the actual operator || in c++ which is equivalent to or. (Note that or is a ISO646 standard and can be used, but depends on preference)

Next we need to ensure ch is added for every character you are checking

After that we should have in a more simple form:

return (ch == '(') || (ch== ')') || (ch == '*') || (ch == '-') || (ch == '+');

Note: We can also do it the way you have by if/else

bool is_operator_match = false;
if (ch == '(') || (ch== ')') || (ch == '*') || (ch == '-') || (ch == '+')
{
    is_operator_match = true;
}

return is_operator_match;
Omid CompSCI
  • 1,861
  • 3
  • 17
  • 29
  • 4
    Minor nitpick: "_Replace or with the actual operator || in c++_" Why? `or` is an [alternative representation](https://en.cppreference.com/w/cpp/language/operator_alternative) of `||` operator. "_which is equivalent to or._" Hence, in fact, it is `or` that is equivalent to `||`. – Algirdas Preidžius Sep 12 '19 at 22:29
  • @AlgirdasPreidžius no it is not... Not all compilers support that and it is not part of the C++ standard. At least not that I have heard/seen. Reading the article I don't see it saying it is part of the standard, I don't think all compilers handle that. – Omid CompSCI Sep 12 '19 at 22:30
  • 3
    "_Not all compilers support that and it is not part of the C++ standard._" Then why is it listed in C++ standard section 5.12 [lex.operators]? – Algirdas Preidžius Sep 12 '19 at 22:32
  • @AlgirdasPreidžius, interesting, did not know it was part of the standard. Looks like it was originated from Standard C in 1995. – Omid CompSCI Sep 12 '19 at 22:36
  • @OmidCompSCI Note that in C the alternative tokens are macros defined in a header, while in C++ they are keywords. – eerorika Sep 12 '19 at 22:38
  • [It can be used interchangeably](https://en.cppreference.com/w/cpp/language/operator_logical). –  Sep 13 '19 at 00:35