conditional (ternary) operator not providing answer that I expected.

I have as basic a question as I think has ever been asked on this forum. I have searched through the conditional/ternary operator responses on this forum and still can't understand why I'm having the following problem. I am learning C++ from a book and have typed the following into my NetBeans 6.9 ide on mac snow leopard OS:
int x = 5;
float y = 5.5;
cout << (x > y) ? x : y;

when I compile this the response is: 0.

When I enter:
if (x > y)
cout << x;
else
cout << y;

I get what I am expecting which is: 5.5

Can anyone help me understand why I get 0 with the conditional operator?

Thank you in advance for your assistance.
Try this:
cout <<((x>y)?x:y)<<endl;
Or:
cout <<(x>y?x:y)<<endl;
It works in my compiler...
Last edited on
The << operator has a higher precedence than the ?: operator
Thank you both for the info! Surrounding the cond stmt with the parentheses did the trick!
Topic archived. No new replies allowed.