Mega Code Archive

 
Categories / C / Code Snippets
 

Use getchar() to read user choices

#include <stdio.h> int main(void) { int x, y; char ch; printf("Choice:\n"); printf("Add, Subtract, Multiply, or Divide?\n"); printf("Enter first letter: "); ch = getchar(); printf("\n"); printf("Enter x: "); scanf("%d", &x); printf("Enter y: "); scanf("%d", &y); if(ch=='A') printf("%d", x+y); if(ch=='S') printf("%d", x-y); if(ch=='M') printf("%d", x*y); if(ch=='D' && y!=0) printf("%d", x/y); return 0; }