请考虑以下代码:
#include <stdio.h>
#include <math.h>
#define INFTY 1000.0
#define EPS 0.0001
#define XRANGE 2
#define URANGE 2
float absf(float);
float E(float,float,float,float,float,float);
int main(){
FILE* out;
float T,R,G,ALPHA,STEP,x,u;
char filename [45];
int c;
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
printf("Enter the dimensionless parameters greater or equal to 0:\nT = ");
scanf("%f",&T);
do { c=getchar(); } while ( c != '\n');//flush stdin
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
printf("R = ");
scanf("%f",&R);
do { c=getchar(); } while ( c != '\n');
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
printf("G = ");
scanf("%f",&G);
do { c=getchar(); } while ( c != '\n');
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
printf("alpha = ");
scanf("%f",&ALPHA);
do { c=getchar(); } while ( c != '\n');
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
printf("step size (recommended:0.001 to 0.01): ");
scanf("&f",&STEP);
do { c=getchar(); } while ( c != '\n');
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
if((T<0)||(R<0)||(G<0)||(ALPHA<0)||(STEP<0)) return 1;//crash if <0
sprintf(filename,"%.4g_%.4g_%.4g_%.4g_%.5g.dat",T,R,G,ALPHA,STEP);
out = fopen(filename,"w");
fprintf(out,"#T=%.3e R=%.3e G=%.3e alpha=%.3e\n",T,R,G,ALPHA);
//do the job
fclose(out);
return 0;
}
这里的问题是它似乎完全忽略了应该输入步长的最后scanf()
;它只需要记忆中的任何垃圾并与它一起使用,通常只是一个非常小的数字。这个输出很有特色:
0.000000 0.000000 0.000000 0.000000 72406190763540480.000000
Enter the dimensionless parameters greater or equal to 0:
T = 1
1.000000 0.000000 0.000000 0.000000 72406190763540480.000000
R = 1
1.000000 1.000000 0.000000 0.000000 72406190763540480.000000
G = 1
1.000000 1.000000 1.000000 0.000000 72406190763540480.000000
alpha = 1.2
1.000000 1.000000 1.000000 1.200000 72406190763540480.000000
step size (recommended:0.001 to 0.01): 0.01
1.000000 1.000000 1.000000 1.200000 72406190763540480.000000
很明显,除了最后一个变量之外,还可以覆盖每个变量。甚至在最后scanf()
完成之前就创建了该文件。将步长输入移到顶部会产生另一个问题:
step size (recommended:0.001 to 0.01): 0.01
Enter the dimensionless parameters greater or equal to 0:
T = R = 1
G = 1
alpha = 1.2
^C
在这种情况下,STEP
实际上并未被0.01覆盖,但仍然是一个非常小的数字。
我已经查找了类似的案例,但似乎没有任何广泛的问题,如输入中的流氓空格。我按照建议here stdin
后scanf()
正在刷新{。}}。
我期待着任何建议。提前谢谢。
答案 0 :(得分:0)
您使用了&#39;&amp;&#39;代替&#39;%&#39;在scanf中(&#34;&amp; f&#34;,&amp; STEP);
#include <stdio.h>
#include <math.h>
#define INFTY 1000.0
#define EPS 0.0001
#define XRANGE 2
#define URANGE 2
float absf(float);
float E(float,float,float,float,float,float);
int main(){
FILE* out;
float T,R,G,ALPHA,STEP,x,u;
char filename [45];
int c;
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
printf("Enter the dimensionless parameters greater or equal to 0:\nT = ");
scanf("%f",&T);
do { c=getchar(); } while ( c != '\n');//flush stdin
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
printf("R = ");
scanf("%f",&R);
do { c=getchar(); } while ( c != '\n');
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
printf("G = ");
scanf("%f",&G);
do { c=getchar(); } while ( c != '\n');
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
printf("alpha = ");
scanf("%f",&ALPHA);
do { c=getchar(); } while ( c != '\n');
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
printf("step size (recommended:0.001 to 0.01): ");
用%f替换&amp; f,它将起作用 的scanf(&#34;&安培; F&#34;,&安培; STEP);
do { c=getchar(); } while ( c != '\n');
printf("%f %f %f %f %f\n",T,R,G,ALPHA,STEP);//debug
if((T<0)||(R<0)||(G<0)||(ALPHA<0)||(STEP<0)) return 1;//crash if <0
sprintf(filename,"%.4g_%.4g_%.4g_%.4g_%.5g.dat",T,R,G,ALPHA,STEP);
out = fopen(filename,"w");
fprintf(out,"#T=%.3e R=%.3e G=%.3e alpha=%.3e\n",T,R,G,ALPHA);
//do the job
fclose(out);
return 0;
}