C++ question

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Say you let:
int x = 4;
char y = 4;

why does
cout << (x == y);
print 1 when:

cout << x << &quot; &quot; << y;
prints out 4 and some weird ascii symbol

yet if you change y to '4' you get 0 for the equality test and they both print out as 4 when printed out individually.
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,003
126
The == operator only takes the face value of the character but does not look up the coresponding ASCII value. The << operator has been overloaded so that it does the ASCII lookup.
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Cool, thanks man wish my CSE book would've said that in the first place.
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Actually the == operator returns a boolean value, 0 = false and 1 = true. So when you use cout << (x == y); it is the same as cout << true;


:)atwl
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,003
126
Actually the == operator returns a boolean value, 0 = false and 1 = true.

And? What does that have to do with it?

I was referring to the method of how it calculates the boolean value. It only takes the raw value of the character instead of doing the ASCII lookup.
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
int x = 4;
char y = 4;


Sorry I didn't notice y is type char. That's why I didn't understand what you were trying to say.


:)atwl