Another new guy asking about that blasted pi program

Hi all! I'm new here (obviously), have done some searching on how to write a program that displays pi using a loop that cycles a certain amount of times (defined by the user).

Now, I've seen code out there that does this, yes, but it is useless to me. Really, they use libraries I don't know, and reference things I'm not quite familiar with yet, so I though I'd hit the book and try to figure it out for myself. It's been a pain typing one line at a time and building it, but especially after seeing the way people who just "want the answer" are righteously given the boot, I figure it would be best to come prepared.

Now, here is my code....

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
long iter;
cout<<"How many iterations of Pi do you want to calculate?";
cin>>iter;

double get_pi (long iter);
{
double i=0; //used in the loop
double d=1.0; //denotes the demoninator
double a; //used for the final answer
while (i<=iter);
{
d=d+2.0*-1.0;
a=a+(1/d);
i++;
}
cout<<a*4.0;
}
}



I've got one problem, and for the life of me, can't figure out what I'm doing wrong. I'm using VS2008 Professional that is from the school, so I don't know if that helps or not. I've fixed the numerous problems I previously had and refined it down to one line of error:

pi2.cpp(22) : warning C4700: uninitialized local variable 'a' used

Now granted, I'm new here, but isn't what I did in the function double get_pi initializing the variable? Doubtful, hence the error. But, the program has no problem referencing the double d (giggity), hence my frustration.

Any help would be greatly appreciated! Heck... I can't even see if this code even works until I figure this problem out.
Give the variable a a value when you created it. As double a = 0; A lot like you did for variables d and i. Problem should then be resolved.
you did not define the variable a in that program so the computer says this line of code throws an error
a=a+(1/d);
because you are using a before it is defined so its value could be 10 or 10000000 or 439034 because that segment of memory in the ram could be a piece of a picture or a word doc or a web page, c++ does not default to 0 as undeclared values.
also, your main function does not have a return value im not sure if you left it out of the code you posted, but u will need that for your int main.
...I hereby turn in my programming card. Can't believe I freakin' missed that!

The bad news is my program doesn't work. Well, figured that out at least... doesn't output anything, and I think that it is getting stuck in the loop since it doesn't even self-close when starting it with debugging.

Thanks much! Hopefully I'll figure out where the heck I'm failing at this soon enough.
Threw the return in there, forgot to add it in. The program is still "locking up" before it can finish.

I thought about doing this using a 'for' syntax, and am going to give that a shot, since maybe I'll have better luck than with a 'while' one.
Got it running! Displays output and all. Changed over to a 'for' loop. But, my math is all messed up, since it is giving me numbers that are WAY off from Pi (like -14, 8, etc).

I'll keep working on it. Thanks for the help in pointing me in the right direction. If there's anything else, I'll let you know, and post the final code for constructive criticism.
Yes! Managed to get it right! Let's call it the "Ballmer Peak" (google it for a fun laugh).


#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;



int main()
{
long i;
double num;
double pi=0;

cout << "Let's calculate pi!"<< endl;
cout << "Please enter the number of iterations you want the computer to run..." << endl;
cin >> num;
cout << endl;

for (i=1; i<= num; i++)
{
if (i%2)
pi=pi+(1.0/(2.0*i-1));
else
pi=pi-(1.0/(2.0*i-1));
}

pi=pi*4;


cout << "Pi is going to be equal to " << setprecision(20) << pi << endl << endl;


return 0;
}


Any tips on how to clean it up? I think it looks pretty trim, but could be wrong.
Again, thanks for the advice all!
There is a library called math.h u can include and play around with. U can also use functions if you want the program to look better and expand your programming knowledge.
EDIT: also If the variable "I" is only used for the for loop that u can declare it in the scope of the for loop like this. For(int I = 1; I != Num; I++)
Last edited on
Try this .......




#include <iostream>
#include <math.h>
using namespace std;

int main()
{

double iter;
double RESULT;
cout<<"How many iterations of Pi do you want to calculate?" << endl;
cin>>iter;
RESULT = iter * M_PI;
cout << RESULT << endl;
system("PAUSE");
}
do not do a system command PLEASE. that is a bad thing to get into, if you want the program to stay running do something like
cin.ignore();
I will keep that in mind.
by the way thanks for your advise
Last edited on
Topic archived. No new replies allowed.