How to write the following condition with a ternary operator using C++
int condition1, condition2, condition3;
int / double result; //int or double
....
std::cout << ( condition1: result1 : "Error" )
<< ( condition2: result2 : "Error" )
<< ( condition3: result3 : "Error")...;
Depends on what type is
result1, result2etc.expressionC ? expression1 : expression2isn’t valid for all types ofexpression1andexpression2. They must necessarily be convertible to a common type, roughly speaking (exact rules and exceptions can be read in the standard). Now, ifresults are strings, then you do it like this:But if results are integers, for example, you can’t do it.
HTH