c - Printing min and max using a EOF loop -


i having difficulty writing c program reads real numbers standard output , determines whether number max or min compared other numbers read previously. program should read real number until eof occurs. below attempt @ problem:

#include <stdio.h> int main() {     double maximum = 0, minimum = 0, number;     while (number != eof) {         printf("enter number: ");         scanf("%lf", &number);         printf("%d", eof);         if (number > maximum) {             maximum = number;         }         else if (number < minimum) {             minimum = number;         }     }     printf("\n max number is: %lf", maximum);     printf("min number is: %lf", minimum);     return 0; } 


challenge i'm not allowed use arrays.

there several issues code:

  1. number not initialized before first iteration.
  2. scanf expects argument pointer, pass in value.
  3. also, don't think minimum 0 (what negatives?), , maximum shouldn't 0.
  4. what if numbers same? you'd not right min

there many ways check end of input, chose use explicitly feof

try code:

#include <float.h> #include <stdio.h> int main() {     double maximum = -dbl_max, minimum = dbl_max, number;     while (!feof(stdin)) {         printf("enter number: ");         scanf("%lf", &number);         if (number > maximum) {             maximum = number;         }         if (number < minimum) {             minimum = number;         }     }     printf("\n max number is: %lf", maximum);     printf("min number is: %lf", minimum);     return 0; } 

still, need improve error management.