sequence question

hi im trying to write a program in the following sequence:

1+(1/4)+....+(1/n^2) but my program doesnt add the term before the entered term..please help..not sure what im doing wrong here
here my code:

#include <iostream>
#include <cmath>


int main ()

{
using namespace std;

int n;
int x;
double sum;

do
{
cout << "Please enter a positive integer...(or 0 to exit)"<<endl;
cin >> n;

for (x=n; x<=n; x++)
{
while (x<=n){

n+=x;

sum = 1 + 1/(pow (x, 2));

x++;

cout << "Sum of the series = " << sum << endl;
}
}
}
while (n!=0);


return 0;
}
You probably don't want a while-loop and a for-loop for this program. Just keep the outer most do-while and one of those other (inner) loops.

(If I might infer from your code: you don't seem to quite "get" the for-loop, so trash it and just use the while one; you can try the for loop and a smaller program with hard coded values so you can better understand it.)
Topic archived. No new replies allowed.