Estimate Pi problems!

Dec 5, 2011 at 3:14pm
When I run my program it asks the user for input but doesn't calculate the value!

Here is my code:

#include<iostream>
#include<cmath>

using namespace std;

double EstimatePie(int terms);

int main()
{int terms;
cout << "Enter the desired number of terms for the summation.";
cin >> terms;
EstimatePie(terms);
return 0;
}

double EstimatePie(int terms)
{int value, Pie;
cout << "Enter an integer to be evaluated.";
cin >> value;
if(value>0)
{Pie = value * (4 - (4/3) + (4/5) - (4/7) + (4/9) - (4/11));}
return Pie;}

I need some help if anyone has any ideas.
Dec 5, 2011 at 3:49pm
And how do you expect to be able to tell if it's working or not? What output are you expecting to the screen?

Also, what do you think (4/3) equals? It equals 1. Dividing one int by another gives you an int. Likewise all your other (int/int).

int value, Pie;
Given that you know pi is not an integer, why try to use an integer value to store it? That's just silly.
Last edited on Dec 5, 2011 at 3:50pm
Dec 5, 2011 at 3:57pm
STORING PIE IS NEVER SILLY.
Dec 5, 2011 at 4:04pm
It is when you could be eating it instead.
Dec 5, 2011 at 5:01pm
quick question: I tried something like this with a formula that was supposed to produce pi to something like the 7th power. I did double pi and it only showed two dec points
Dec 5, 2011 at 5:04pm
The default precision of cout [on your system] is set to 2. Use setprecision to change it:
For one line: http://www.cplusplus.com/reference/iostream/manipulators/setprecision/
For runtime: http://www.cplusplus.com/reference/iostream/ios_base/precision/
Dec 5, 2011 at 9:38pm
I took what you said into consideration and this is what I have come up with, but nothing has changed. Anymore pointers to get me going in the right direction?

#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

double EstimatePie(int terms);

int main()
{int terms;
cout << "Enter the desired number of terms for the summation.";
cin >> terms;
cout << setprecision(10) << EstimatePie(Pie);
return EstimatePie(Pie);
}

double EstimatePie(int terms)
{double value, Pie;
cout << "Enter an integer to be evaluated.";
cin >> value;
if(value>0)
{Pie = value * (4.0 - (4.0/3.0) + (4.0/5.0) - (4.0/7.0) + (4.0/9.0) - (4.0/11.0));}
return Pie;}
Dec 5, 2011 at 9:58pm
cout << setprecision(10) << EstimatePie(Pie);

Here you are trying to call the function EstimatePie, passing it the variable Pie... which does not exist. Read the compiler errors. It will tell you this without you having to come to ask us.
Last edited on Dec 5, 2011 at 9:58pm
Dec 5, 2011 at 10:24pm
I do not get any compiler errors.

My program runs it just doesn't return a value for Pie.
Dec 5, 2011 at 10:47pm
Ok, in your function 'EstimatePie' you pass it the parameter 'terms' but where are you actualy using this number?

Dec 5, 2011 at 10:54pm
I do not get any compiler errors.

Then either:

1) You are not compiling the code in your post above, or

2) You're not looking at the error list, or

3) You have a magic compiler that somehow magically fixes errors for you. By magic. Or,

4) You have a hopelessly broken compiler that doesn't work and doesn't tell you about it.


I'd guess option 1, but maybe option 2.

As an aside, is your program supposed to come up with an estimate of the value of Pi? If it is, your algorithm is wrong.
Last edited on Dec 5, 2011 at 11:01pm
Dec 6, 2011 at 12:22am
Yeah, that is what I though
Dec 6, 2011 at 10:07am
Dev-C++ strikes again?
Dec 6, 2011 at 4:15pm
My vote is for option 3
Dec 6, 2011 at 4:43pm
Dec 6, 2011 at 4:49pm
More digits! MORE DIGITS I SAY!
Dec 6, 2011 at 4:53pm
Thank you for the solution oriented post Kyon!
Topic archived. No new replies allowed.