加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:22:41 所属栏目:百科 来源:网络整理
导读:#include stdio.h#include stdlib.h #define f(x) (1 / (x*x+1))int main(){ double a,b,h,x,y; printf("Enter a,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 = f(x);
#include <stdio.h>
#include <stdlib.h> 
#define f(x) (1 / (x*x+1))



int main(){
    double a,b,h,x,y;

    printf("Enter a,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",y );
        x +=h;

     }


    system("Pause");
    return(0);  

}

解决方法

问题出在你的scanf上:

scanf(" %lf %lf %lf ",&h);
                   ^

scanf需要查看下一个非空格来确定这个“0或更多空格”的结束,所以你必须给第四个值(它可以是垃圾 – 只要它不是空格),以便scanf终止输入.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读