-6

I have the following code snippet:

if (w)
{
    if (b)
    {
        if (c)
            cout << "great!";
        else
            cout << "3";
    }
    else
    {
        cout << "2";
    }
}
else
{
    cout << "1";
}   

I have to use the ternary operator instead. I thought that

cout << w ? b ? c ? "great" : "" + 3 : "" + 2 : "" + 1;  

would be fine. It isn't.

Edited:

Even if I use the following code snippet, it still doesn't work properly:

cout << (w ? b ? c ? "great!" : "3" : "2" : "1");

Please try to run the following code:

int w =1, b = 1, c = 0;
if (w)
{
    if (b)
    {
        if (c)
            cout << "great!";
        else
            cout << "3";
    }
    else
    {
        cout << "2";
    }
}
else
{
    cout << "1";
}   

cout << "  ";

cout << w ? (b ? (c ? "great" : "3") : "2") : "1";

Why is the output 3 1 ?

How should I correctly "transform" the code ?

Andrei
  • 217
  • 2
  • 12

4 Answers4

2

Use braces {} and () accordingly should work.

--> Start using braces in the original code, then replace the "if" with the ternary operator and replace the braces with brackets.

You will get something like this:

cout << (w ? (b ? ( c ? "great" : "3") : "2") : "1");

not sure I really prefer this approach though...

Daniel
  • 1,041
  • 7
  • 13
  • You don't need parentheses inside the ternary operators, e.g. `cout << (w ? b ? c ? "great" : "3" : "2" : "1");` – Paul R Apr 21 '15 at 11:41
  • No problem - you can lose the other parentheses too - they are harmless, but redundant. – Paul R Apr 21 '15 at 11:45
  • Let's say that it's easier to read that way (at least for me). I usually prefer easier reading over shorter text. – Daniel Apr 21 '15 at 11:47
  • Sure - I find it more readable without the parentheses, but everyone's entitled to their opinion. ;-) – Paul R Apr 21 '15 at 11:48
2

The equivalent ternary expression would be

cout << (w ? b ? c ? "great!" : "3" : "2" : "1");

However if you are having a hard time figuring out how to write such a nested expression, coworkers are going to have a hard time reading it. Try to prefer readability and correctness to terse one-liners.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Now add a couple of brackets for clarity and I could almost use this approach... (almost...) – Daniel Apr 21 '15 at 11:39
  • 1
    doesn't do what you think it does though... `<<` got higher precedence so you start off with `(cout << w)`. ternary operators should always use parentheses. – AliciaBytes Apr 21 '15 at 11:39
0

That is not how you convert an integer to a string in C++. "" + 3 has a completely different meaning (which is a terrible useless one that just gives everybody who uses it nasal demons).

If you correctly converted your integer to a std::string there would be nothing wrong with your code.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • He would still have a problem with operator precedence, even if he fixed this. It needs to be: `cout << (w ? b ? c ? "great" : "3" : "2" : "1");`. – Paul R Apr 21 '15 at 11:46
0

Try this,

 w ?
 (
        b ?
        (
                c ?
                (
                        cout << "great!"
                )
                : cout << "3"
        )
        : cout << "2"
 )
 : cout << "1";
fatihbozik
  • 51
  • 1
  • 4