简体   繁体   English

C++ 错误:“(”标记之前的预期标识符

[英]C++ error: expected identifier before "(" token

int nn1,nn2;
for (int i=1;i<=m;i++) if (A[i]>minim) && (A[i]<maxim) nn1++;
for (int j=1;j<=n;j++) if (B[j]>minim) && (B[j]<maxim) nn2++;
if (nn1>nn2) cout<<"1";
if (nn1<nn2) cout<<"2";
if (nn1=nn2) cout<<"0";

return 0;
}

can anybody give me a suggestion please why the compiler shows the error?任何人都可以给我一个建议,为什么编译器会显示错误?

In these if statements there are absent external parentheses在这些 if 语句中没有外括号

for (int i=1;i<=m;i++) if (A[i]>minim) && (A[i]<maxim) nn1++;
for (int j=1;j<=n;j++) if (B[j]>minim) && (B[j]<maxim) nn2++;

I think that there should be我认为应该有

for (int i=1;i<=m;i++) if ( (A[i]>minim) && (A[i]<maxim) ) nn1++;
for (int j=1;j<=n;j++) if ( (B[j]>minim) && (B[j]<maxim) ) nn2++;

And the loops look suspeciously.循环看起来很可疑。 Take into account that array indices start from 0. So for example if you have an array of size N then the valid range of indices is [0, N-1]考虑到数组索引从 0 开始。例如,如果您有一个大小为N的数组,那么索引的有效范围是[0, N-1]

And you forgot to initialize nn1 and nn2 .你忘了初始化nn1nn2

It seems that you mean the following看来你的意思是以下

int nn1 = 0, nn2 = 0;

for ( int i = 0; i < m; i++ ) 
{
    if ( ( A[i] > minim ) && ( A[i] < maxim ) ) nn1++;
}

for ( int i = 0; i < n; i++ ) 
{
    if ( ( B[i] > minim ) && ( B[i] < maxim ) ) nn2++;
}

if ( nn1 > nn2 ) cout << "1";
else if ( nn1 < nn2 ) cout << "2";
else cout << "0";

Fix the brackets in if condition.在 if 条件下固定括号。 Something like if (condition1 && condition2).类似于 if (condition1 && condition2)。 I also suggest using curly brackets for every branch condition.我还建议对每个分支条件使用大括号。

for (int i=1;i<=m;i++) {
    if ((A[i]>minim) && (A[i]<maxim)) { 
        nn1++;
    }
}
for (int j=1;j<=n;j++) {
    if ((B[j]>minim) && (B[j]<maxim)) {
        nn2++;
    }
}

Take care of paranthesis when dealing with ifs :处理ifs时注意括号:

if ( (B[j]>minim) && (B[j]<maxim) )

   |                              |
   V                              V

 Add these 

So, your code should be:所以,你的代码应该是:

int nn1,nn2;
for (int i=1;i<=m;i++) if ( (A[i]>minim) && (A[i]<maxim) ) nn1++;
for (int j=1;j<=n;j++) if ( (B[j]>minim) && (B[j]<maxim) ) nn2++;
if (nn1>nn2) cout<<"1";
if (nn1<nn2) cout<<"2";
if (nn1=nn2) cout<<"0";

return 0;
}

Missing parentheses around the whole condition :整个条件周围缺少括号:

for (int i=1;i<=m;i++) if((A[i]>minim) && (A[i]<maxim)) nn1++;
//                       ^                            ^

You forgot the ( ) in if statement.Try this :你忘记了 if 语句中的 ()。试试这个:

int nn1,nn2;
for (int i=1;i<=m;i++) if ((A[i]>minim) && (A[i]<maxim)) nn1++;   //note extra ()
for (int j=1;j<=n;j++) if ((B[j]>minim) && (B[j]<maxim)) nn2++;   //note extra ()
if (nn1>nn2) cout<<"1";
if (nn1<nn2) cout<<"2";
if (nn1=nn2) cout<<"0";
return 0;
}

In C++,there should be a complete "()" after the "if" and what in "()" is the condition.在C++中,“if”后面应该有一个完整的“()”,“()”中的什么是条件。 So,the code you showed is if(A[i]>minim) && (A[i]minim) && (A[i]所以,你展示的代码是 if(A[i]>minim) && (A[i]minim) && (A[i]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM