#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
usingnamespace std;
int main()
{
double acc;
int i;
double pi = 0;
double pie = 3.14159265359;
cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
cin>>acc;
do
{
for (i = 1; i<= 100000; i++)
pi += 4*(1/i)/((2*i)+1);
}
while(abs(pie - pi) > acc);
cout<<pi<<endl;
system("Pause");
return 0;
}
Nothing ever prints inside your while loop, so the fact that you're getting a blank prompt indicates that your while loop is not ending.
Try putting the cout<< pi << endl inside your while loop.
Second, note that ( 1 / i ) performs integer division, which will almost always make the return be 0 if i is greater than 1 due to truncation. You probably want (1.0 / i).
int main()
{
double acc;
int i;
double pi = 0;
double pie = 3.14159265359;
cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
cin>>acc;
system("Cls");
do
{
for (i = 1; i<= 100000; i++)
pi += 4*(1.0/i)/((2.0*i)+1);
cout<<pi<<endl;
}
while(abs(pie - pi) > acc);
cout<<pi<<endl;
system("Pause");
return 0;
}
fixed the infinite loop, but now im getting 2.4548 for every input of "acc". is my equation wrong? I feel it is but i don't know how to add in the 'switching' negative sign into the equation.
Instead of using acc as a double, I would suggest using it as an int then use the setprecision(acc) within the cout to do the precision the user wants. This function is in the iomanip file.
well, my exact assignment is to create a program that outputs the value of pi to the given accuracy (input as .001, .01, etc.) by using a do-while loop and the leibniz series.
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
usingnamespace std;
int main()
{
double acc;
double i;
double pi = 0;
double pie = (16*(atan(1.0/5.0))-(4*atan(1.0/239)));
cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
cin>>acc;
system("Cls");
do
{
for (i = 1; i<= 100000; i++)
pi +=4*pow(-1.0, i+1)/((2.0*i)+1);
}
while(abs(pie - pi) > acc);
cout<<pi<<endl;
system("Pause");
return 0;
}
Having a cout statement can't make something be infinite.
Your loop is infinite whether or not you have a cout statement in there.
Your "pi" number is divergent and increases well past 3.14, so it's a problem with your line 26.
Also note that your for loop is kinda useless in this regard, since it's already wrapped around a loop. In fact it might be messing you up since it's resetting the value of i each time.
Still getting same problem, don't know where i am going wrong. Is it still my "pi"? Still not ever getting a value after i run the program. If i put the cout<<pi; inside my do-while, it outputs the infinite loop of it running the equation. How do i get the 'correct' value of pi out of the loop?
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
usingnamespace std;
int main()
{
double acc;
double i = 0;
double pi = 0;
double pie = (16*(atan(1.0/5.0))-(4*atan(1.0/239)));
cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
cin>>acc;
system("Cls");
do
{
pi +=4*(pow(-1.0, i+1)/((2.0*i)+1));
i++;
}
while(abs(pie - pi) > acc);
cout<<pi<<endl;
system("Pause");
return 0;
}
the i < 100000 was arbitrary, I don't need/want it in there now. I just want it to stop when acc is at the desired.
I think i have it, but why is it always giving me 5 digits past the decimal? is that just default, because i haven't put in fixed, setw(), setprecision(), yet.
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
usingnamespace std;
int main()
{
double acc;
int i = 0;
double pi = 0;
double pie = (16*(atan(1.0/5.0))-(4*atan(1.0/239)));
cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
cin>>acc;
system("Cls");
do
{
pi +=4*(pow(-1.0, i)/((2.0*i)+1));
i++;
system("Cls");
cout<<pi;
}
while(abs(pie - pi) > acc);
system("Pause");
return 0;
}
sorry, one last question. I put a loop in so the program could be repeated, it works fine with everything except when i put in a string that is not "yes" or "Yes", i.e. no, asdf, yea, etc. one character input exits the program, and the yes or Yes repeats, but everything else sends it into an infinite loop or something.
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
usingnamespace std;
int main()
{
double acc;
int i = 0;
double pi = 0;
double pie = (16*(atan(1.0/5.0))-(4*atan(1.0/239)));
string ans = "yes";
while("yes" == ans || ans == "Yes")
{
cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
cin>>acc;
system("Cls");
do
{
pi +=4*(pow(-1.0, i)/((2.0*i)+1));
i++;
system("Cls");
cout<<fixed<<setw(10)<<setprecision(6)<<pi;
}
while(abs(pie - pi) > acc);
cout<<endl<<"Would you like to run this program again? Yes or yes for yes, anything else for no"<<endl;
cin>>ans;
cout<<endl;
}
system("Pause");
return 0;
}