当我尝试为我的程序提供3个输入时,它需要4才能正常运行.这是为什么?

Cor*_*ple 3 c++ dev-c++

#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);  

}
Run Code Online (Sandbox Code Playgroud)

iBu*_*Bug 6

问题出在你的scanf上:

scanf(" %lf %lf %lf " , &a, &b, &h);
                   ^
Run Code Online (Sandbox Code Playgroud)

scanf需要查看下一个非空格以确定此"0或更多空格"的结尾,因此您必须提供第4个值(它可以是垃圾 - 只要它不是空格)scanf来终止输入.

如果您在Windows上,可以在新行上按Ctrl-Z并按Enter键.这将向程序发送EOF,也可以终止输入.(我想你是在Windows上,因为我system("pause")在你的程序中看到了)

  • @CorrurtedDisciple您可以单击此答案旁边的灰色勾号将其标记为"已接受". (2认同)