Estimate Pi problems!

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.
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
STORING PIE IS NEVER SILLY.
It is when you could be eating it instead.
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
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/
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;}
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
I do not get any compiler errors.

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

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
Yeah, that is what I though
Dev-C++ strikes again?
My vote is for option 3
More digits! MORE DIGITS I SAY!
Thank you for the solution oriented post Kyon!
Topic archived. No new replies allowed.