Nguythang

getdouble

Nov 19th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <time.h>
  8. #include <ctype.h>
  9.  
  10. void clear(void)  
  11. {
  12.     while(getchar()!='\n');
  13. }
  14. double getdouble(double min, double  max)
  15. {
  16.     double value;
  17.     char after;
  18.     int  rc,keeptrying = 1;
  19.     do
  20.     {
  21.         printf("Enter a whole number\n"
  22.         "in the range [%lf,%lf] : ", min, max);
  23.         rc = scanf("%lf%c", &value, &after);
  24.         if (rc == 0) {
  25.            printf("**No input accepted!**\n\n");
  26.            clear();
  27.         } else if (after != '\n') {
  28.            printf("**Trailing characters!**\n\n");
  29.            clear();
  30.         } else if (value < min || value > max) {
  31.             printf("**Out of range!**\n\n");
  32.         } else
  33.             keeptrying = 0;
  34.     } while (keeptrying == 1);
  35.    
  36.     //printf("%lf", value);
  37.     return value;
  38. }
  39. main()
  40. {
  41.     double a;
  42.     printf(" a = %10.10lf",getdouble(0,100));
  43. }
Advertisement
Add Comment
Please, Sign In to add comment