Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper Ternary Operator Format

This is what I have. I'm unsure how to properly write it. I tried Googling around, but to no avail. Please don't cringe too much:

cout << (guess > randomNumber) ? "\nWhoops! Try again!\n You guessed higher than the random number!\n\n"
        : (guess < randomNumber) ? "\nWhoops! Try again!\n You guessed lower than the random number!\n\n"
        : "";

What I'm wanting it to do is this:

    // Gives hint that inputted number is higher or lower
    // than inputted number
    if (guess > randomNumber)
        cout << "\nWhoops! Try again!"
        << " You guessed higher than the random number!\n"
        << endl;
    else if (guess < randomNumber)
        cout << "\nWhoops! Try again!"
        << " You guessed lower than the random number!\n"
        << endl;

Any help would be appreciated. I just want to learn how I can write my programs to be more efficient and smaller. Would appreciate any feedback.

like image 552
Red One Avatar asked Mar 06 '16 01:03

Red One


2 Answers

Put some brackets around the entire expression, otherwise you're going to end up printing boolean values:

int guess = 10;
    int randomNumber = 9;

    cout << (guess > randomNumber) ? "\nWhoops! Try again!\n You guessed higher than the random number!\n\n"
            : (guess < randomNumber) ? "\nWhoops! Try again!\n You guessed lower than the random number!\n\n"
            : "" ;

// Output: 1

Proper code:

int guess = 10;
    int randomNumber = 9;

    cout << ( (guess > randomNumber) ? "\nWhoops! Try again!\n You guessed higher than the random number!\n\n"
            : (guess < randomNumber) ? "\nWhoops! Try again!\n You guessed lower than the random number!\n\n"
            : "" ); // Notice the brackets!

/*Output:
Whoops! Try again!
You guessed higher than the random number!*/
like image 157
058 094 041 Avatar answered Oct 29 '22 16:10

058 094 041


more efficient

Not related to what you're doing there (if by "efficient" you mean better runtime characteristics).

smaller

A humble goal, but not if readability falls short because of it (and even less so if, because of the syntactic complexity ... missing parentheses ... the end result is wrong).

Remember: Code is written for humans to read.

You should probably stick with the if and else approach you're also showing in the question. That said, an IMHO "good" approach (if you really need to abstract over this) would be to pack it into some function:

template<class T, class X, class Y, class Z>
void comp_if(T value, T reference, X less, Y equal, Z greater) {
  if (value < reference) less();
  else if (value > reference) greater();
  else equal();
}

Used like

// missing real macros a lot
comp_if(foo, bar,
  []() {cout << "less"; },
  []() {cout << "equal";},
  []() {cout << "greater"});

Whether this really helps with readability is a choice I leave up to the reader to make.

like image 43
Daniel Jour Avatar answered Oct 29 '22 15:10

Daniel Jour