これがコードで、文字を入力したり、ドット、fe 0,1、0,000045 の代わりにコンマを使用したりするとループします。それを回避するにはどうすればよいですか?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double pi=0.0;
double eps = 0.0;
double summand=0.0;
int k=0;
char c;
int main(){
printf("Please enter EPS:\n");
scanf("%lf", &eps);
ここからループを開始しますが、文字を入力したりカンマを使用すると、常にループします
while ((eps>=1)||(eps<=0)) {printf("\nPlease enter a Precision Interval that is less than 1 and
bigger than 0\n(do not use comma):\n");
scanf("%lf", &eps);
}
これは、プログラムが実行する必要がある実際のコードです。
do{
summand = 1/pow(16,k);
pi += (double)summand*((double)4/(8*k+1)-(double)2/(8*k+4)-(double)1/(8*k+5)-(double)1
/(8*k+6));
printf("pi:%0.17lf EPS:%0.17lf summand:%0.17lf\n", pi, eps, summand);
k++;
}while(summand>eps);
//pi = 3.141592653589793238462643;
printf("Pi is equal to %0.17lf\n\n\n\n", pi);
//-------------------------------------------------------------------------------
これは、プログラムを再起動するかどうかです。
do
{
printf("Do you want to restart the program? Y(es) or N(o)\n");
scanf(" %c",&c); // note the space
}while((c!='y')&&(c!='Y')&&(c!='n')&&(c!='N'));
if ((c=='y')||(c=='Y')) { system("cls"); main();}
else
{
return 0;
}
}