#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> ivec;
int ival;
while (cin >> ival)
ivec.push_back(ival);
for ( int sum = 0, ix = 0;ix < ivec.size();++ix)
sum += ivec[ix];
int average = sum / ivec.size(); //切记,这里会造成编译错误,因为 sum 只作用于上述的 for 循环中
cout << "Sum of " << ivec.size()
<< " elements:" << sum
<< ".Average :" << average << endl;
}
需将:
for ( int sum = 0, ix = 0;ix < ivec.size();++ix);
改为:
int sum = 0;
for (int ix = 0;ix < ivec.size();++ix);
这样,程序便可以运行了。