how compare a char?

when i do:
IsOperator(strinput[uintStringIndex+1])=='='
why i get these warning:
"comparison of constant ''='' with boolean expression is always false [-Wbool-compare]"
???
IsOperator, I assume, returns a bool. This can only be true (1), or false (0). '=' is a char, which evaluates to some positive number greater than 1 (in this case, 61).

0 or 1 can never equal 61, so the expression will always be false!

I think you're making this more complicated than necessary. Why not just do,
if (strinput[index+1] == '=') { /* logic... */ }
or
bool isEqualsSign = (strinput[index+1] == '=');

BTW, make sure not to go out of bounds of your string, those +1's can be dangerous.
Last edited on
is much more than sign.
and yes i have changed the function from boolean to char:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
char IsOperator(char chr)
{
    if(chr=='=')
        return chr;
    else if(chr=='\\')
        return chr;
    else if(chr=='/')
        return chr;
    else if(chr=='*')
        return chr;
    else if(chr=='+')
        return chr;
    else if(chr=='-')
        return chr;
    else if(chr=='^')
        return chr;
    else if(chr=='[')
        return chr;
    else if(chr==']')
        return chr;
    else if(chr=='{')
        return chr;
    else if(chr=='}')
        return chr;
    else if(chr=='>')
        return chr;
    else if(chr=='<')
        return chr;
    else if(chr=='(')
        return chr;
    else if(chr==')')
        return chr;
    else if(chr=='.')
        return chr;
    else if(chr==',')
        return chr;
    else if(chr==';')
        return chr;
    return 0;
}

zero means false, now i have the control.
thank you so much for all
That's a really misleading function name, just so you know... usually IsFoo() functions return bools.

Btw, here's a way to simplify a bool IsOperator function (if returning a bool, not char).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Example program
#include <iostream>
#include <string>
#include <algorithm>

bool IsOperator(char ch)
{
    std::string operators = "=\\/*+-^[]{}<>().,;";
    auto is_operator = [ch](char known_operator){return ch == known_operator;};
    return std::any_of(operators.begin(), operators.end(), is_operator);
}

int main()
{
    if (IsOperator('=')) { std::cout << "= is an operator" << std::endl; }
}
Last edited on
is these case, do you have a better name?
Well, I'm not sure. I would rework whatever it is you're doing. I don't understand what you're trying to accomplish with such a function, regardless of the name. See edit of a better bool IsOperator, btw.

The way you're using IsOperator seems redundant, I don't see why you don't just do
(strinput[i] == '=').
Last edited on
thank you so much... i don't know too much about strings
Ganado: That IsOperator() is terrible.
1
2
3
4
5
6
7
8
bool IsOperator(char ch){
    static const char operators[] = "=\\/*+-^[]{}<>().,;";
    static const auto n = sizeof(operators) - 1;
    static const auto beg = operators;
    static const auto end = operators + n;

    return std::find(beg, end, ch) != end;
}
thank you so much.. now it's more easy to understand
Noted.

Edit: However, I wouldn't say "terrible". std::find is still linear complexity. Sure, my example allocates the string each call, I agree that could be made static. I also agree that "std::find" makes more sense from an English-language point of view than "any_of", but I don't see this as a huge deal. If we really wanted to pre-optimize this, I'd go with binary_search, http://www.cplusplus.com/reference/algorithm/binary_search/ and hardcode the char array as sorted.
Last edited on
I just wanted to get rid of the allocation, but if we're really going to optimize, why stop at O(log n)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Operators{
    static const auto min = std::numeric_limits<char>::min();
    static const auto max = std::numeric_limits<char>::max();
    static const auto n = max - min + 1;
    
    std::vector<bool> data;
    
    static unsigned to_index(char c){
        return (unsigned)((int)c - (int)min);
    }
public:
    Operators(): data(n, false){
        static_assert(n <= 65536, "Whoa! What kind of chars you got there?");
        
        static const char ops[] = "=\\/*+-^[]{}<>().,;";
        for (auto i = ops; *i; i++)
            data[to_index(*i)] = true;
    }
    Operators(const Operators &) = delete;
    Operators(Operators &&) = delete;
    const Operators &operator=(const Operators &) = delete;
    Operators &&operator=(Operators &&) = delete;
    
    bool operator()(char c) const{
        return data[to_index(c)];
    }
};

Operators IsOperator;
Last edited on
Brilliant :)
Topic archived. No new replies allowed.