Hello,I am new for C++.The following are the codes I wrote to calculate the sum of 2^n. The result is 2046 after running the program.That is correct. But when I change the condition to n<=100,the result comes with -2,which is obviously wrong.Then I tried different n and found if n<=30,the right result can be obtained but if n>=31 the result is always -2.Can someone tell me where my mistake lies and how I can solve it? Thank you.
#include<iostream>
int main()
{
int n=1,a=1,sum=0;
while(n<=10) {
a=2*a;
sum=sum+a;
n=n+1;
}
std::cout <<"The result is "<<sum
<<std::endl;
return 0;
}