untitledtorrent
untitledtorrent

Reputation: 59

Need assistance with very simple C++ for loop

for( int i = 0; i <= 10; i++ )
{
    cout << setw(2) << i << setw(10) << ( i <= 5 ? cout <<" = Chipotle" : cout << " = McDonalds" ) << endl;
}

So, I expected the output to be:

 0 = Chipotle
 1 = Chipotle
 2 = Chipotle
 3 = Chipotle
 4 = Chipotle
 5 = Chipotle
 6 = McDonalds
 7 = McDonalds
 8 = McDonalds
 9 = McDonalds
10 = McDonalds

(Don't worry about the setw formatting)

Instead, my IDE gave me:

 = Chipotle 0  0x602208                                                                                                             
 = Chipotle 1  0x602208                                                                                                             
 = Chipotle 2  0x602208                                                                                                             
 = Chipotle 3  0x602208                                                                                                             
 = Chipotle 4  0x602208                                                                                                             
 = Chipotle 5  0x602208                                                                                                             
 = McDonalds 6  0x602208                                                                                                            
 = McDonalds 7  0x602208                                                                                                            
 = McDonalds 8  0x602208                                                                                                            
 = McDonalds 9  0x602208                                                                                                            
 = McDonalds10  0x602208

Where did I went wrong?

Upvotes: 2

Views: 123

Answers (5)

David K
David K

Reputation: 3132

When i <= 5 is true, this

cout << i << ( i <= 5 ? cout <<" = Chipotle" : cout << " = McDonalds" ) << endl

evaluates like this:

cout << i << (cout << " = Chipotle") << endl

I omitted the setw manipulators here to make the code easier to read, which would affect spacing in the result but makes no difference otherwise. The result is the same as this sequence of operations:

cout << " = Chipotle"; // the thing in `()` gets evaluated first
cout << i;
cout << cout; // because (cout << " = Chipotle") evaluates to cout
cout << endl;

which is exactly what you see for the first few lines. cout itself is printed out as 0x602208. After that you get McDonalds instead of Chipotle.

If you just write cout once, at the left end of your output expression, you will get the output you want.

Upvotes: 1

Eduard Rostomyan
Eduard Rostomyan

Reputation: 6546

This is more precise and readable approach.

for( int i = 0; i <= 10; i++ )
{
    cout << setw(2) << i << setw(10) ;
    if(i<= 5)
    {
        cout <<" = Chipotle"<<endl;
    }
    else
    {
        cout << " = McDonalds" << endl;
    }
}

Upvotes: 0

Ajitesh
Ajitesh

Reputation: 17

You do not have to write cout again inside the if condition you have written using the ?: operators. Just place the string what you want to print without the cout

Upvotes: 0

NathanOliver
NathanOliver

Reputation: 180490

You are trying to cout a cout.

( i <= 5 ? cout <<" = Chipotle" : cout << " = McDonalds" )

becomes either

cout <<" = Chipotle" 
//or 
cout << " = McDonalds"

So you are trying to do

cout << setw(2) << i << setw(10) << cout <<" = Chipotle" 
//or
cout << setw(2) << i << setw(10) << cout <<" = McDonalds" 

Which is incorrect. You need to change you code to

( i <= 5 ? " = Chipotle" : " = McDonalds" )

Which will expand to

cout << setw(2) << i << setw(10) << " = Chipotle" 
//or
cout << setw(2) << i << setw(10) << " = McDonalds" 

Upvotes: 3

Jarod42
Jarod42

Reputation: 217135

You display cout itself, remove cout in ternary operator:

cout << setw(2) << i << setw(10) << ( i <= 5 ? " = Chipotle" : " = McDonalds" ) << endl

Upvotes: 4

Related Questions