Your problem is your using a typecast from double to int when you say that n = pow(x,i) and n = 1 + n... Notice that x and i are both double and when you assign them to n, you switch the type from double to int, which results in 2 in C++. Even though, 38 iterations will yield 2 on any programming language...
Code:
#include <iostream>
#include <math>
using namespace std;
int main(){
double n = 0;
double x,i;
x = 0.5;
for(i=0; i <=38; i++){
n += pow(x,i);
}
cout << "The sum results in: " << n << "." << endl;
return 0;
} It takes only 19 iterations to arrive at 2.