That is not allowed by C++. That is why it's giving error.
By the way, you can do this by defining a local struct
inside the function itself,
struct local {int loop; long result; };
for (local power = {0,1}; power.loop <= 10 ; power.loop++, power.result *= 2 )
{
cout << 2 <<"^" <<power.loop << "=" << power.result << endl;
}
Working code here at ideone : http://www.ideone.com/ELT4a
Or you can simply define the struct
in the for
loop itself, like this,
for ( struct {int loop; long result; } power = {0,1}; power.loop <= 10 ; power.loop++, power.result *= 2 )
{
cout << 2 <<"^" <<power.loop << "=" << power.result << endl;
}
Sample : http://www.ideone.com/X1MdC