fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. static void skip_input(FILE *stream)
  6. {
  7. int c;
  8. do {
  9. c = fgetc (stream);
  10. } while (c != EOF && c != '\n');
  11. }
  12.  
  13. int main()
  14. {
  15. int a;
  16. int r;
  17. char junk[2] = {'\0'};
  18.  
  19. printf("Please enter a value: ");
  20.  
  21. for (;;) {
  22. r = scanf("%d%1[^\n]", &a, junk);
  23.  
  24. if (r == EOF)
  25. return EXIT_FAILURE;
  26. if (r != 1) {
  27. printf("Number must be numeric!\n");
  28. skip_input(stdin);
  29. } else if (a < 0) {
  30. printf("Number must be postive\n");
  31. skip_input(stdin);
  32. } else {
  33. printf("Everything is goood\n");
  34. break;
  35. }
  36. }
  37.  
  38. return EXIT_SUCCESS;
  39. }
Success #stdin #stdout 0.01s 1724KB
stdin
1a
1
stdout
Please enter a value: Number must be numeric!
Everything is goood