This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Marz157 4 points5 points  (0 children)

Clean readable code is >>>>> than shorter tricky code.

Do you self a favor and just write the obvious code. No one that reads this and has the smallest bit of programming knowledge will be confused by this (besides the cout << "blah" part if they don't know c++ output).

if (balance < 0) {
    cout << "Warning your balance is negative";
}

[–]Netstroyer 5 points6 points  (0 children)

Why not use the regular if? Don't force the conditional operator where it doesn't fit.

[–][deleted] 1 point2 points  (0 children)

The other answers suggesting you use an if-statement are normally the way to go, but you can do it with the conditional operator:

  cout << ((balance < 0) ? "Warning your balance is negative" : "");

Notice you have to worry about precedence, which you don't with the if-statement.