Hi Everyone,
For a couple weeks, I have been developing a pi approximation program and making little upgrades. Right now, the program asks for an input of iterations and then prints out the approximation. Now, I want to have every iteration printed out into an excel file with an approximation accurate to

`````` .000001 [50,000 iterations]
``````

However, I realized that the current setup of my program does not work well, so I think some rework is in order.
So my goal is to eliminate the input process; and instead, base the iterations on accuracy, not input. That is what I would like some help with. The problem is that I do not know how to set up a for loop to go until that accuracy point is reached. The ofstream part I am somewhat proficient at so I will add all that in later.

``````#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
//Declare variables
float x;
float y;
int counter;
int n;
int swap;

//Get input
cout << "Please input the number of terms in the series to be calculated." << endl;
cout << "(at least 700 for recognizable accuracy)" << endl;
cin >> n;

x = 0;
swap = 1;
counter = 0;

//Loop
for (int counter = 0; counter != n; ++counter)
{
x += swap * (1.0 / (1 + counter * 2));
swap *= -1;
}

x = 4 * (x);
//Averaging most recent terms
counter = counter - 2;
y = ((swap * (1 / (1 + counter * 2))) + x);

cout << setprecision(15) << "My best approximation of PI is " << y << endl;

cin.get();
cin.get();

//Exit program
return 0;
}
``````

Ok. Updating that I figured everything out. Posting code to help others who experience similar problems.

``````#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>

using namespace std;

int main()
{
//Declare variables
long double x;
long double y;
int counter;
int n = 50000;
int swap;
int count;
ofstream report;
x = 0;
swap = 1;
counter = 0;

//Open output file
report.open("pi.csv");
if (report.fail())
{
cerr << "Error opening input file.";
cin.get();
cin.get();
exit(1);
}
else
cout << "The report has been created in Microsoft Excel." << endl;

//Loop
for (int counter = 0; counter < n; ++counter)
{
x += 4 * (swap * (1.0 / (1 + counter * 2)));
swap *= -1;
report << x << endl;
}
//Averaging most recent terms
counter = counter - 2;
y = ((swap * (1 / (1 + counter * 2))) + x);

report << setprecision(15) << y << endl;

cin.get();
cin.get();

//Exit program
report.close();
return 0;
}
``````

There are manny aproximations for pi, try to find the fastest one to human kind.
Look for the best algorithm, and then try to ask the expert in numerical math...