Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: menu.c
- * Description: simple menu implementation to demo flush problem
- * Author: pcyu16
- * Last update: 2010/12/31 20:35
- *
- * expected result: input 2 numbers, then print a menu,
- * ask for output destination, then deal with user input option
- * actually result: after input 2 numbers, program exit with error message predefined
- */
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int a, b;
- FILE *fout;
- // title
- puts("The A+B Problem");
- // read numbers
- printf("please input a b >> ");
- scanf("%d %d", &a, &b);
- // menu
- puts("a. output to standard output");
- puts("b. output to out.txt");
- printf("\n>> ");
- fflush(stdout);
- int option;
- option = getchar();
- // seletion statment by option read
- if(option == 'a')
- fout = stdout;
- else if(option == 'b')
- fout = fopen("out.txt", "w");
- else{
- // printf("key value: %d\n", option);
- fprintf(stderr, "ERROR! INVALID OPTION!!\n");
- exit(1);
- }
- // output calculated result
- fprintf(fout, "%d + %d = %d\n", a, b, a+b);
- return 0;
- }
Add Comment
Please, Sign In to add comment