#include stdio.h#include stdlib.h #define f(x) (1 / (x*x+1))int main(){ double a,b,h,x,y; printf("Enter a, b, h: "); scanf(" %lf %lf %lf " , a, b, h);// I ask for 3 inputs but the programm needs 4 to run...why is that? x = a; while(xb) { y
#include <stdio.h>
#include <stdlib.h>
#define f(x) (1 / (x*x+1))
int main(){
double a,b,h,x,y;
printf("Enter a, b, h: ");
scanf(" %lf %lf %lf " , &a, &b, &h);
// I ask for 3 inputs but the programm needs 4 to run...why is that?
x = a;
while(x<b)
{
y = f(x);
printf("%lf %lf \n", x ,y );
x +=h;
}
system("Pause");
return(0);
}
问题出在你的scanf上:
scanf(" %lf %lf %lf " , &a, &b, &h);
^
scanf需要查看下一个非空格来确定这个“0或更多空格”的结束,所以你必须给第四个值(它可以是垃圾 – 只要它不是空格),以便scanf终止输入.
如果您在Windows上,可以在新行上按Ctrl-Z并按Enter键.这将向程序发送EOF,也可以终止输入. (我想你是在Windows上,因为我在你的程序中看到了系统(“暂停”))
