Aug 17, 2014 at 11:15pm Aug 17, 2014 at 11:15pm UTC
Just would like some quick clarification on the ternary operator. The book i have doesnt cover it. I just wanna see if a number is Neg or Pos and spit back negative or positive.
int vPos == 1 ? cout << "Positive" << endl; : cout << "Negative" << endl;
I get errors (obviously), im pretty sure my syntax is off or im just not using the operator correctly.
Aug 17, 2014 at 11:19pm Aug 17, 2014 at 11:19pm UTC
Remove the int
and semicolon:
(vPos == 1)? (cout << "Positive" << endl) : (cout << "Negative" << endl);
Alternatively:
cout << ((vPos == 1)? "Positive" : "Negative" ) << endl;
Aug 18, 2014 at 12:13am Aug 18, 2014 at 12:13am UTC
Thank you both i appriciate it!