需要非常简单的C ++ for循环帮助

时间:2015-04-24 13:28:38

标签: c++ for-loop

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

所以,我希望输出为:

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

(不要担心setw格式化)

相反,我的IDE给了我:

 = 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

我哪里出错?

5 个答案:

答案 0 :(得分:4)

您自己显示cout,删除三元运算符中的cout

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

答案 1 :(得分:3)

你正在尝试一个cout。

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

成为

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

所以你要努力

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

哪个不对。您需要将代码更改为

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

将扩展为

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

答案 2 :(得分:1)

i <= 5为真时,此

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

评估如下:

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

我在这里省略了setw操纵器,使代码更容易阅读, 这将影响结果中的间距 但不然没有区别。 结果与此操作序列相同:

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

这正是您在前几行中看到的内容。 cout本身打印为0x602208。 之后,您获得McDonalds而不是Chipotle

如果您只是在输出表达式的左端写一次cout, 你会得到你想要的输出。

答案 3 :(得分:0)

您不必在使用?:运算符编写的if条件内再次编写cout。只需将字符串放在您想要打印的内容中,而不使用cout

答案 4 :(得分:0)

这是更精确和可读的方法。

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