By trying, I came to know that it is necessary to put parentheses around a conditional operator in a cout statement. Here a small example:
#include <iostream>
int main() {
int a = 5;
float b = (a!=0) ? 42.0f : -42.0f;
// works fine
std::cout << b << std::endl;
// works also fine
std::cout << ( (a != 0) ? 42.0f : -42.0f ) << std::endl;
// does not work fine
std::cout << (a != 0) ? 42.0f : -42.0f;
return 0;
}
The output is:
42
42
1
Why are these parentheses necessary? The resulting type of the conditional operator is known in both cases, isn't it?
See Question&Answers more detail:os