# Help with simple program

• 03-22-2006
nik2007
Help with simple program
Can someone please check if this program is correct.... i'm trying to write a PI approximation....but i can't get the output that i have to obtain....... suggestions?????

Code:

``` #include <iostream> #include <iomanip> using namespace std; int main() {       double pi=0, y=1;       int num, i;       char ch;       pi = 4 - 4/3 + 4/5 - 4/ 7 + 4/ 9 - 4/11 + 4/13 - 4/15;       pi = 0;       y = 1;       do         {           cout<<"How many terms do you want to use?\n";           cin>>num;         for (i = 1; i<=num; i++)                 if (i%2 == 0)                                         pi = pi - 4/y;                 else                 pi = pi + 4/y;                 cout<<pi,;                 y = y + 2;                                 cout<<"Do you want another approximation? please enter Y or N\n"; cin>>ch; }while (ch!= 'n' && ch!= 'N'); return 0; }```
• 03-22-2006
should put you in right direction

Code:

```#include <iostream> #include <iomanip> using namespace std; int main() {       double pi, y=1;       int num, i;       char ch;       pi = 4.0 - 4.0/3.0 + 4.0/5.0 - 4.0/7.0 + 4.0/9.0 - 4.0/11.0 + 4.0/13.0 - 4.0/15.0;       y = 17.0;       do         {           cout<<"How many terms do you want to use?\n";           cin>>num;         for (i = 1; i<=num; i++)         {                 if (i%2 == 0)                                         pi = pi - 4/y;                 else                 pi = pi + 4/y;                 y = y + 2;         }                                cout<<pi;         cout<<"Do you want another approximation? please enter Y or N\n";         cin>>ch; }while (ch!= 'n' && ch!= 'N'); return 0; }```
• 03-22-2006
Tonto
You have to watch your whitespace. You only need to print the number after you are out of the loop. The number of terms needs to be very high for accurate results.

Code:

```        do         {                 cout<<"How many terms do you want to use?\n";                 cin>>num;                 for (i = 1; i<=num; i++)                 {                         if (i%2 == 0)                         {                                 pi = pi - 4/y;                         }                                                 else                         {                                 pi = pi + 4/y;                         }                         y = y + 2;                 }                                         cout<<pi<<endl;                 cout<<"Do you want another approximation? please enter Y or N\n";                 cin>>ch;                 pi = 0;         } while (ch!= 'n' && ch!= 'N');```
• 03-22-2006
nik2007
thank you so much for your help.....i can see now where i made my mistakes....

thanks!!!!!!
• 03-23-2006
sand_man
You also set pi back to 0 after assigning it.

Code:

```      pi = 4 - 4/3 + 4/5 - 4/ 7 + 4/ 9 - 4/11 + 4/13 - 4/15;       pi = 0;       y = 1;```
• 03-23-2006
WaltP
Quote:

Originally Posted by Tonto
You have to watch your whitespace.

IMAO, so do you. TABs indent too far. Use 3-4 spaces instead ;)