Proper Ternary Operator Format

Viewed 153

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.

2 Answers
Related