1. ## Please check this loop

I appologize for putting this code up again, but I can not find where it is not looping this time again and I don't think it's a simple mistake like last time.
Code:
```//The program checks to see how many parameters it takes to near Pi using the equation 4X(1-1/3+1/5-1/7+1/9-1/11....)
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double pi = 3.14195;
double a = 1, p = 1;
double n = 3, sign = -1;
bool check = true;
int finish = 0, t = 0;
do
{
while (check)
{
a += sign*(1.0/(n)); //the equation
sign = -sign; // reversing the signs
t++; // counting how many parameters are needed to near Pi
n+= 2;
if (a*4 <= pi + 1*(pow(10,-(p+1))) && a*4 >= pi - 1*(pow(10, -(p+1)))) //checking if it is close enough to Pi
check = false; //breaking loop if it is close enough to Pi
}
cout << fixed << setprecision(p);
cout << a*4 << " " << t <<  endl;
finish++; // adding 1 for each loop completed up to the limit of 6
p++; //adding 1 to the power and precision decimal places
t = 0; //reseting counter
a = 1; //reseting a
check = true; //switching check back to true to loop while again
}while(finish < 6);
return 0;
}```
There are some other errors with it such as a is displaying too inaccurate of a number for the single loop that it does but I first want to get the loop to work then I could fix the problem with a. 2. heres a warning you could fix:

Code:
`Untitled1.cpp [Warning] passing `double' for converting 1 of `std::_Setprecision std::setprecision(int)'` 3. Fixed the warning, just set another int variable, but that still didn't fix the huge problem heh. I think the while loop is failing to loop and I don't know why. 4. The problem isn't what I think you think it is The series you're using to describe pi is very slow to get within the restraints you give it, so I imagine that you're surprised when it takes until p=96 to get past the first step. Maybe not, I don't really know, since you didn't say why you think the while loop is failing. It works fine for me... 5. Decrypt: Thanks for checking, I'm not sure. Maybe it does work fine but it takes a long time to get the answer, I'm not really sure. I am not sure if the 96 is even correct. My cousin did a code, much different and much more ugly, crude, and all around confusing to read, but it worked. I understand what he did, I was just trying to simplify it, however I am not sure if it worked. If you confirm it works, do you eventually get all the answers? 6. OK, I take it back, it's not just that. You're resetting a at the end of every run through the while loop, but not n. So at the beginning of the second loop, a = 1, but n still equals 197. Then a+=sign*1/n: 1 + (1/197), then 1 + (1/197) - (1/196)...etc.

That's not the series you're looking for. 7. Ohhhh! That's probly it, let me try that one out. 8. BAM! That was it! ha ha ha thanks! Now I just need it to display 3.0, not 3.2 the first time and so forth to 3.14195

Edit: Quick edit, reading your reply also I should set sign back to -1 as well just in case sign ended in a possitive number, that woudl throw the entire thing off as well. 9. Throw some tracing messages into your code to track the numbers to figure out the solution.

Also... I suggest switching these loops over to for loops. It would be quite a bit more readable.

Code:
```//The program checks to see how many parameters it takes to near Pi using the equation 4X(1-1/3+1/5-1/7+1/9-1/11....)
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double pi = 3.14195;
double a = 1, p = 1;
double n = 3, sign = -1;
bool check = true;
int finish = 0, t = 0;
do
{
cout << "p is " << p << '\n';
while (check)
{
a += sign*(1.0/(n)); //the equation
sign = -sign; // reversing the signs
t++; // counting how many parameters are needed to near Pi
n+= 2;

cout << "a * 4 " << a * 4 << '\n';
cout << "pi + 1 * pow(10, -(p+1))" << pi + 1 * pow(10, -(p+1)) << '\n';
cout << "pi - 1 * pow(10, -(p+1))" << pi - 1 * pow(10, -(p+1)) << '\n' << endl;

if (a*4 <= pi + 1*(pow(10,-(p+1))) && a*4 >= pi - 1*(pow(10, -(p+1)))) //checking if it is close enough to Pi
check = false; //breaking loop if it is close enough to Pi
}
cout << fixed << setprecision(p);
cout << a*4 << " " << t <<  endl;
finish++; // adding 1 for each loop completed up to the limit of 6
p++; //adding 1 to the power and precision decimal places
t = 0; //reseting counter
a = 1; //reseting a
check = true; //switching check back to true to loop while again
}while(finish < 6);
return 0;
}```
Code:
```a * 4 3.13118
pi + 1 * pow(10, -(p+1))3.15195
pi - 1 * pow(10, -(p+1))3.13195

a * 4 3.1519
pi + 1 * pow(10, -(p+1))3.15195
pi - 1 * pow(10, -(p+1))3.13195

3.2 96
p is 2.0
a * 4 4.0
pi + 1 * pow(10, -(p+1))3.1
pi - 1 * pow(10, -(p+1))3.1

a * 4 4.0
pi + 1 * pow(10, -(p+1))3.1
pi - 1 * pow(10, -(p+1))3.1

a * 4 4.0
pi + 1 * pow(10, -(p+1))3.1
pi - 1 * pow(10, -(p+1))3.1

a * 4 4.0
pi + 1 * pow(10, -(p+1))3.1
pi - 1 * pow(10, -(p+1))3.1```
The value of a is not changing appreciably, because you are forgetting to reset the value of n to 3 at the start of each loop. 10. Thank you everyone who has helped me fine tune this code, the final code looks like this:
Code:
```#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double pi = 3.14195;
double a = 1, p = 2;
double n = 3, sign = -1;
bool check = true;
int finish = 1, t = 0, e = 1;
do
{
while (check)
{
a += sign*(1.0/(n));
sign = -sign;
t++;
n+= 2;
if (a*4 <= pi + 1*(pow(10,-(p))) && a*4 >= pi - 1*(pow(10, -(p))))
check = false;
}
cout << fixed << setprecision(e);
cout << a*4 << " " << t <<  endl;
finish++;
p++;
e++;
t = 0;
a = 1;
n = 3;
sign = -1;
check = true;
}while(finish < 6);
return 0;
}```
The only problem I am having is it is rounding where I now don't want it to round. I am not sure if this is solveable, I am only a novice, but if anyone has a suggestion to fix that minute problem, please post a reply, thanks.  11. Throw some tracing messages into your code to track the numbers to figure out the solution.
That or use a debugger. I like to run through the code myself first, (it helps, I think, to understand what's going on) but if I'm stumped I use a debugger to see what's really happening. 12. I dunno, I like to read the code and all too. Never really used the debugger yet. In truth I am just starting to learn C++. This is my first class, and I know I make lots of small, stuipid mistakes, but ask you all can plainly see I understood the code, I just needed some more eyes to spot the error, just like proof reading a paper. I'll try to use the debugger and see if that helps more, but a human touch is usually better heh. 13. but a human touch is usually better
That's true a lot of the time, but make sure you're putting full effort into solving it yourself before you post. This was good, you told us about what you thought was wrong, etc. If you can add in the things you've tried on your own, more people will be willing to look into it for you. Good luck. 14. Just so you know, that's one of the slowest pi algorithms in existence. You might want Machin or Ramanujan series, or the simpler (actually simpler than the above code) Gause-Legendre algorithm, which converges very quickly. Popular pages Recent additions 