Guest User

Untitled

a guest
Jul 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. /*
  2. * File: menu.c
  3. * Description: simple menu implementation to demo flush problem
  4. * Author: pcyu16
  5. * Last update: 2010/12/31 20:35
  6. *
  7. * expected result: input 2 numbers, then print a menu,
  8. * ask for output destination, then deal with user input option
  9. * actually result: after input 2 numbers, program exit with error message predefined
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. int main()
  15. {
  16. int a, b;
  17. FILE *fout;
  18.  
  19. // title
  20. puts("The A+B Problem");
  21.  
  22. // read numbers
  23. printf("please input a b >> ");
  24. scanf("%d %d", &a, &b);
  25.  
  26. // menu
  27. puts("a. output to standard output");
  28. puts("b. output to out.txt");
  29. printf("\n>> ");
  30. fflush(stdout);
  31.  
  32. int option;
  33.  
  34. option = getchar();
  35.  
  36. // seletion statment by option read
  37. if(option == 'a')
  38. fout = stdout;
  39. else if(option == 'b')
  40. fout = fopen("out.txt", "w");
  41. else{
  42. // printf("key value: %d\n", option);
  43. fprintf(stderr, "ERROR! INVALID OPTION!!\n");
  44. exit(1);
  45. }
  46.  
  47. // output calculated result
  48. fprintf(fout, "%d + %d = %d\n", a, b, a+b);
  49. return 0;
  50. }
Add Comment
Please, Sign In to add comment