fork download
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5. int option;
  6. int ch;
  7.  
  8. while(1) {
  9.  
  10. if ( scanf("%d", &option) != 1) {
  11. printf("Invalid input. Input must be a number between 1-4. Try again.\n");
  12. while ( (ch=getchar())!=EOF && ch != '\n'); // Read out all the chars from input
  13. continue;
  14. }
  15. else if (option<1 || option >4) {
  16. printf("Invalid input. Input must be a number between 1-4. Try again.\n");
  17. continue;
  18. }
  19.  
  20. printf("option: %d\n", option);
  21. // Valid input
  22. // Do whatever you want here with the 'option'
  23.  
  24. break; //To break the infinite loop after reading a valid number
  25. }
  26.  
  27. }
  28.  
Success #stdin #stdout 0s 2116KB
stdin
3
stdout
option: 3