C++ Multiple statements for conditional operator -
i'm trying use conditional statement 1 thing in 1 condition 2 things if other condition applies.
consider following:
( h >= 0 && h < 24 ? hour = h : hour = 0, cout << "invalid hour detected\n")
if "h" set 25, sets "hour" 0 correctly. if "h" set 12, correctly sets "hour" 12.
the problem outputs "invalid hour detected" both true , false conditions. want output if conditions aren't met.
essentially, i'm wondering if possible in conditional statement 2 things 1 condition.
also tried:
( h >= 0 && h < 24 ? hour = h : hour = 0 && cout << "invalid hour detected\n")
but didn't run cout on either case.
if really want this, add proper parentheses , invert order of assignment , output insertion (when using comma operator, value of left expression discarded):
( h >= 0 && h < 24 ) ? ( hour = h ) : (std::cout << "invalid hour detected\n", hour = 0);
however, advice make code readable , abandon kind of coding style.
Comments
Post a Comment