简体   繁体   中英

When i try to give 3 inputs to my program , it needs 4 to run properly. Why is that?

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

}

The problem is with your scanf:

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

scanf need to see the next non-whitespace to determine the end of this "0 or more whitespaces", so you'll have to give the 4th value (it can be garbage - as long as it's not whitespace) for scanf to terminate the input.

If you're on Windows, you can hit Ctrl-Z on a new line and press Enter. This will send an EOF to the program, which can also terminate the input. (I suppose you're on Windows because I see system("pause") in your program)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM