### Equals Operator Question

Whats the best way to say if (a==b==c==d==e==f==8){} ?

Thanks,

Eric
Last edited on
a == b && b == c && ... && f == 8
if (a+b+c+d+e+f == 48){ ... }? :p
Computergeek, that would not work, the following could be legit, too:
a = 16
b = 0
c = 8
d = 24
e = -8
f = 8
But that would fall out of bounds for his origional question which was what is the best way to test if all of the integers are equal. jsmith answered it I was entering a joke, hence the emoticon. Take another look I don't even test if any of the integers are the same in my line, so it doesn't even address the same question.
I know, I just wanted to point out to the OP that it doesn't work. We have no proof of any degree of skill from his side (no offense intended, erickulcyk). I just don't want people to use wrong code. If I offended you in any way, I'm sorry, I was just doing this for clarity's sake.
Abuse parentheses!
`if( ( (a==b) == (c==d) ) == ( (e==f) == (f==8) ) ){ ... }`
Yeah. I joking too..
Sure, you're joking, only thing that needs pointing out is that you're comparing integers in the most inner ones, and then keep comparing bools. If all are equal, except for e and f, and f not being 8, it would return true.
Last edited on
 ``123456789101112131415`` ``````int ar[6] = {a,b,c,d,e,f}; bool equal = true; for(int i = 0; i < 6; ++i) { if(ar[i] != 8) { equal = false; break; } } if(equal) { // they all == 8 }``````

Note that I only mention this route as it's very likely a,b,c,d,e,f all should be an array if they're not already.
@Kyon
Oh. you're right. And that's not the only problem. It would return true for a=0 b=0, c=5, d=5, e=f=8. I guess you really can't do this without repeating every variable twice..
Last edited on
 ``1234`` ``````if((a&b&c&d&e&f&8) == (a|b|c|d|e|f|8)) { //... }``````
Another one:
 ``1234567891011121314`` ``````struct INT{ int i; bool ok; INT(){} INT(int I) : i(I), ok(true){} }; INT operator == (INT a, INT b){ INT r; r.ok = a.i == b.i && a.ok; r.i = a.i; return r; }``````

 ``12`` ``````INT a, b, c, d, e, f; if( (a == b == c == d == e == f == 8).ok ){ ... }``````

If only C++0x was here, I would have made an explicit conversion to bool..
While we're at C++0x:

 ``12345678`` ``````#include template bool all_equal(std::initializer_list list) { for (auto it=list.begin()+1;it!=list.end();++it)if (*(it-1)!=*it)return false; return true; } [...] if (all_equal({a,b,c,d,e,f}))...``````
i would do it this way:
 ``1234`` ``````if(a==b)&&(a==c)&&(a==d)&&(a==e)&&(a==f)&&(a==8)) { }``````

That's a syntax error, right there. You need another `(` after the `if`
Topic archived. No new replies allowed.