EOF
must be preceded with a newline else it won't work. However it depends on the OS. The EOF you enter at the end of of the line containing the input is not recognized. From the man page of scanf
-
scanf returns the number of items successfully matched and assigned which can be fewer than provided for, or even zero in the event of an early matching failure. The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs.
Therefore you should check the return value of scanf
for 1
, not EOF
.
#include <stdio.h>
int main(void) {
double numbers = 0, sum = 0;
printf("Enter numbers: ");
while(scanf("%lf", &numbers) == 1) {
sum += numbers;
}
printf("\n %.2lf", sum);
}