Force user to enter a valid response : Console Input Validation « Console « C / ANSI-C






Force user to enter a valid response

  
#include <stdio.h>

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

  printf("Do you want to (Choose the first letter):\n");
  printf("(A)dd, (S)ubtract, (M)ultiply, or (D)ivide?\n");

  do {
    printf("Choice: ");
    ch = getchar();
  } while(ch!='A' && ch!='S' && ch!='M' && ch!='D');
  printf("\n");

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

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

  return 0;
}


           
       








Related examples in the same category