为什么这段代码不能编译

时间:2015-05-14 15:16:54

标签: c++

#include <iostream>
#include<iomanip>
using namespace std; 
int main()
{
    const int NUMROWS=3; 
    const int NUMCOLS=4;
    int i,j; 
    int val[NUMROWS][NUMCOLS]={8,16,9,52,27,6,14,25,2,10};//multiply each element by 10 and display it 
    cout<<"\nDisplay or multiplied elements"; 
    for(i=0; i<NUMROWS;i++)
    {
        val[i][j]=val[i][j]*10;
    }//end of inner loop
    }//end of outer loop
    cout<endl; 
    return 0; 
    }

这些是我收到的错误。我做错了16:5:错误:'cout'没有命名类型 17:5:错误:在'return'之前预期的unqualified-id 18:5:错误:'}'令牌之前的预期声明

5 个答案:

答案 0 :(得分:2)

你错过了你的内循环和你的cout双循环之后缺少第二个胡萝卜。应该看起来像这样:

int main()
{
    const int NUMROWS=3; 
    const int NUMCOLS=4;
    int i,j; 
    int val[NUMROWS][NUMCOLS]={8,16,9,52,27,6,14,25,2,10};//multiply each element by 10 and display it 
    cout<<"\nDisplay or multiplied elements"; 
    for(i=0; i<NUMROWS;i++)
    {
        for(j=0; j<NUMCOLS;j++)
        {
            val[i][j]=val[i][j]*10;
        }//end of inner loop
    }//end of outer loop
    cout<<endl; 
    return 0; 
}

答案 1 :(得分:1)

这里有两个问题:

}//end of outer loop
cout<endl; 

首先,当您的评论说&#34;外部循环结束&#34;时,大括号实际上会关闭main,因此代码不正确。这就是为什么您在cout上收到错误的原因并没有为类型命名。

删除该大括号后,您将收到一个巨大的编译错误:

  

错误:“operator<

中的“std::cout < std::endl”不匹配

因为您在写cout < endl;;

时写了cout << endl

答案 2 :(得分:1)

错别字:

  cout<endl; 

你遗失了<  应该是:

 cout<<endl; 

将上述内容放在main内,目前它超出了main的范围。

其他问题:

 val[i][j]=val[i][j]*10;

j此处未初始化。

  }//end of outer loop

您尚未定义任何外部for loop

答案 3 :(得分:1)

我相信你错过了for索引的j循环:

for(i=0; i<NUMROWS;i++)
{
    for(j=0; j<NUMCOLS;j++)
    {
        val[i][j]=val[i][j]*10;
    }//end of inner loop
}//end of outer loop

另请注意,您遗漏的<字符cout<endl;应为cout<<endl;

答案 4 :(得分:0)

它不能编译,因为它不是有效的C ++程序。

一个问题是你有比关闭括号更多的结束括号。我建议使用源代码编辑器来指示彼此之间的内容。