Use getchar() to read user choices : Console Output Char « Console « C / ANSI-C






Use getchar() to read user choices

  
#include <stdio.h>

int main(void)
{
  int a, b;
  char ch;

  printf("Choice:\n");
  printf("Add, Subtract, Multiply, or Divide?\n");
  printf("Enter first letter: ");
  ch = getchar();
  printf("\n");

  printf("Enter a: ");
  scanf("%d", &a);
  printf("Enter b: ");
  scanf("%d", &b);

  if(ch=='A') printf("%d", a+b);
  if(ch=='S') printf("%d", a-b);
  if(ch=='M') printf("%d", a*b);
  if(ch=='D' && b!=0) printf("%d", a/b);

  return 0;
}

           
       








Related examples in the same category

1.Simple printf demo
2.Output the char's ASCII code
3.Use getche() to receive user selection