Question

double low = 0 , high = 0.0,n = 0.0;
----
printf("Enter the integral limits (low high): ");
scanf("%lf %lf", &low, &high);

printf("%lf%lf",low,high);
printf("Enter the number of subinternals (n): ");
scanf("%lf",&n);

When I test this program (by

    printf("%lf%lf",low,high);) (6th line)

I get values of 0.000000 and 0.000000 for low and high. I initialized them at the beginning of the program and now I'm trying to input values for them.

What am I doing wrong?

{P.S. I'm on a windows 7 laptop. and I've already tried changing the "%f" to "%lf" and back to "%f")

What should I do? (HELP PLEASE!)

EDIT: I'm not sure what just happened. I error checked with

        if (2 != scanf("%lf %lf", &low, &high))
               printf("scanf failed!\n")

and what happened instead was that after my "..(low high): " I entered 1 and 2, and then the program was waiting for more input, and then I entered 1 and 2 again, and this time, it gave the right prinft... not sure what happened?

Output screen: http://s1288.photobucket.com/user/Kdragonflys/media/ss_zps6b85396e.png.html

No correct solution

OTHER TIPS

You need %lf for doubles with scanf, and you also need two of these if you have two doubles to read.

So change:

scanf("%f", &low, &high);

to:

scanf("%lf %lf", &low, &high);

Also change:

scanf("%f",&n);

to:

scanf("%lf",&n);

Note: error checking is always a good idea - scanf returns the number of values successfully processed. So you could check whether the first scanf call is working like this:

if (scanf("%lf %lf", &low, &high) != 2)
    printf("scanf failed!\n");

Try this,

double low = 0.0 , high = 0.0,n = 0.0;
----
printf("Enter the integral limits (low high): ");
scanf("%lf %lf", &low, &high);

printf("%lf %lf",low,high);
printf("Enter the number of subinternals (n): ");
scanf("%lf",&n);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top