i am using g++ (GCC) 4.6.0 and i am having trouble to producing the correct result. given the following simple for loop in c++
void sum(){
int sum;
for(int i=0,sum=0;i<=10;sum+=i,++i);
cout << sum << endl;
}
the output is giving me 0. suppose within the for loop i added
cout << sum << endl;
it give me 0,1,3... until the very last line 0;
I think the reason I am getting 0 is because variable shadowing in the for loop? so I tried ::sum as in ::sum=0, and ::sum +=i. but it complains by the compiler. Also I tried
for(sum=0,int i=0;i<=10;sum+=i,++i);
the compiler also complains about not having primary expression in the first clause in the for loop