play_again0.c 756 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* play_again0.c
  2. * purpose: ask if user wants another transaction
  3. * method: ask a question, wait for yes/no answer
  4. * returns: 0=>yes, 1=>no
  5. * better: eliminate need to press return
  6. */
  7. #include <stdio.h>
  8. #include <termios.h>
  9. #define QUESTION "Do you want another transaction"
  10. int get_response( char * );
  11. int main()
  12. {
  13. int response;
  14. response = get_response(QUESTION); /* get some answer */
  15. return response;
  16. }
  17. int get_response(char *question)
  18. /*
  19. * purpose: ask a question and wait for a y/n answer
  20. * method: use getchar and ignore non y/n answers
  21. * returns: 0=>yes, 1=>no
  22. */
  23. {
  24. printf("%s (y/n)?", question);
  25. while(1){
  26. switch( getchar() ){
  27. case 'y':
  28. case 'Y': return 0;
  29. case 'n':
  30. case 'N':
  31. case EOF: return 1;
  32. }
  33. }
  34. }