注册 登录
编程论坛 C++教室

关于for循环

FrankloveCyy 发布于 2019-06-22 12:31, 2908 次点击
#include<iostream>
using namespace std;

int main()
{
    const int ARRAY_LENGTH= 5;
    int myNums[ARRAY_LENGTH]= {0};
   
    cout<<"Populate array of"<< ARRAY_LENGTH <<"integers"<<endl;
   
    for(int counter=0;counter<ARRAY_LENGTH;+counter)
    {
        cout<<"Enter an integer for element"<<counter<<":";
        cin>>myNums[counter];
    }
    cout<<"DisPlaying contents of the array:"<<endl;
   
    for(int counter=0;counter<ARRAY_LENGTH;++counter)
        cout<<"Element"<<counter<<"="<<myNums[counter]<<endl;
        
        return 0;
 }
问一下大神,为什么这段代码打完,编译不通过,错误信息栏双击开之后就是乱码啊。我是照着书上打的啊,检查几遍了,代码和书上一样的,就是查不出哪里错了,能帮我指出来吗?谢谢了
5 回复
#2
FrankloveCyy2019-06-22 12:32
看的是书叫《21天学通C++,第8版》
#3
FrankloveCyy2019-06-22 12:41
找到问题了,原来是上一个运行窗口没关闭
#4
stargay2019-06-22 17:07
死循环呀
#5
Jason_2019-07-15 21:14
#6
rjsp2019-07-16 09:18
4楼 stargay 说得对.
对于 for(……;+counter)
g++ 报 warning: for increment expression has no effect
vc  报 warning C4552: '+' : operator has no effect; expected operator with side-effect
无论哪个编译器都会给出警告,但若用户视而不见充耳不闻,那神仙也没办法。
1