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

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

}
1 Answers

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)

Related