ok i need a quick help here......
here it's my problem
It's approximate value of π is 3.141592653589793
Below are five different series which can be used to approximate π:

you can se the series here::

http://i44.tinypic.com/2ujtmxw.gif

ask which series he/she wants to use
and the precision (number of digits, up to 10). If after
10000 iterations the series doesn't find the value of π, it
should display an error message.

example:
Which series do you want?
2
How many digits of accuracy ?
5

It will display either an error message or the value of π and
the number of iterations necessary to obtain that value.
In order to round a real number to n decimal places.

My code is here:: but doesn't goes on if staytment.... i can't understand.. how i can round the numbers on if staytment??

int main()
{
char series;
int digits;
double pi1=0;
const double counter1=3.141592653589793;
long int counter=0;



cout << " Which series? (1-5): ";
cin>>series;
cout << " How many digits of accuracy? (<10): ";
cin>>digits;


if (series !='1' && series !='2' && series!='3' && series !='4' && series!='5')
{
cout <<" Error Input....."<<endl;
cout <<" Their are Only 5 series...(1,2,3,4,or5)"<<endl;
cout <<" The program will be terminated in 5 seconds..."<<endl;
Sleep(5000);
}
else 
if (series=='1')
{
cout<<" Series 1"<<endl;
cout<<" --------"<<endl;
cout<<""<<endl;
     for (long int n=1; n<=10000; n++)
		 {
		   pi1 += (4*pow(-1.0, n+1))/(2*n - 1); 
		    if (pi1!=counter1)
			{//if start
			   counter++;
		        }//if end
		   else 
			   
			   if(pi1!=counter1)
			   {
cout<<"Series "<<series<<" is approximate on "<<counter<<" iterations"<<" Series "<<series<<" is "<< fixed <<setprecision(digits) << pi1;
		       cin>>pi1;
			   } 
			   else{ 
				  cout<<"This series dosn't approximeta on p value....change series"<<endl;
}

if this is line the line with the problem

cout<<"Series "<<series<<" is approximate on "<<counter<<" iterations"<<" Series "<<series<<" is "<< fixed <<setprecision(digits) << pi1;

after " is" im not sure what you are trying to do in "<< fixed <<setprecision(digits) << pi1; is this the problem?

if you mean that

if(pi1!=counter1)

sorry i write it wrong, it's

line 39.

if(pi1==counter1)

but didn't works... i don't have the results that i want.. i want to calculate step by step from 1 to 10000 the result of pi of each iterations and the program stop went the result is approximate on value pi if doesn't approximate the program will be error message

example:

if user select 1 series
and
2 digits
then it's approximate...
because we had 3.14
if the user select the 1 series
and
6 digits
then it's not approximate

maybe you could convert pi and the number you are calculating to a string and then compare them. this way you could set the size of the string to be the precision you want.

maybe you could convert pi and the number you are calculating to a string and then compare them. this way you could set the size of the string to be the precision you want.

This is absurd, it will take to me a long time to me...
Can you please give me an example... what you mean...?? Because i thing i don't understand you.....

Thank you........

well you could do this

char * pi = new char[digits + 2];
char * equationResualt = new char[digits + 2];
_gcvt(counter1, digits, pi);
for (long int n=1; n<=10000; n++)
{
pi1 += (4*pow(-1.0, n+1))/(2*n - 1); 
 _gcvt(pi1, digits, equationResualt);
 if (*equationResualt!=*pi)
{//if start
counter++;
 }//if end
else 
 if (*equationResualt==*pi)
{
...

this converts the numbers into strings and then you can check them. also this will help you display the result since its already a string and formated to the precision you want. you will also need to add #include <stdlib.h>

thank you... on line 7.

what the _gcvt do??

it converts a float to string

char *_gcvt( double value, int digits, char *buffer );

value

Value to be converted

digits

Number of significant digits stored

buffer

Storage location for result

the string stores the decimal point so you need to make sure you have room for it thats why i have char * pi = new char[digits + 2]; +1 wouldnt leave space for the null terminator so you have to use +2

Ok thank you very much for your help....

sorry but i made a mistake on line 8 you should use if (strcmp(equationResualt, pi) != 0) and on line 13 you should use if (strcmp(equationResualt, pi) == 0) sorry but it was late and i wasn't thinking strait.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.