fork download
  1. #include <stdio.h>
  2.  
  3. int cont()
  4. {
  5. int c;
  6. do {
  7. int d;
  8. printf("Continue (y/n): ");
  9. if ((c = fgetc(stdin)) < 0) {
  10. fprintf(stderr, "Input failed!\n"); return 0;
  11. }
  12. printf("Input '%c'\n", c);
  13. for (d = c; d != '\n';) {
  14. if ((d = fgetc(stdin)) < 0) {
  15. fprintf(stderr, "Input failed!\n"); return 0;
  16. }
  17. }
  18. } while (c != 'y' && c != 'n');
  19. return c == 'y';
  20. }
  21.  
  22. int main()
  23. {
  24. int i = 0;
  25. do {
  26. printf("Loop iteration %d.\n", ++i);
  27. } while (cont());
  28. /* done */
  29. return 0;
  30. }
Success #stdin #stdout 0s 9424KB
stdin
y
Hello
n
stdout
Loop iteration 1.
Continue (y/n): Input 'y'
Loop iteration 2.
Continue (y/n): Input 'H'
Continue (y/n): Input 'n'