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 ?