Look at your code:
int x(5),a[7],i(0);
for(i=0;i<8;i++)
{
a[i] = 0;
...
}
So your array
a
has 7 elements, and you write 0 into the first 8.
When you write the 8th element, it overruns the array, and writes into the next memory location - which contains the variable
i
. So after the 8th time round the loop, it looks at
i
and sees it is zero, so it goes round again... And again...