I am having an interesting problem in scanf. It inputs the integers successfully but overlooks the character. In debugging I saw a strange value placed in the character variable. Kindly help me--

#include <stdio.h>
int main()
{
  int a,b;
  char op;
  printf("Welcome To Command Based Calculator");
 // printf("\n");
  printf("Now enter two numbers");
  scanf("%d",&a);
  scanf("%d",&b);
  printf("Select the desired operator");
 // printf("%d",a);
  // printf("%d",b);
   scanf("%c",&op);
    printf("%c\n",op);
  printf("Its working");
  scanf("%c", &op);
  printf("the value in op--%c\n",op);
    switch(op)
     {
       case '+':
       printf("%d + %d = %d", a, b, a+b);
       break;
       case'-':
       printf("%d - %d = %d", a, b, a-b);
       break;
       case'*':
       printf("%d * %d = %d", a, b, a*b);
       break;
       case'/':
       printf("%d / %d = %d", a, b, a/b);
       break;
       case'%':
       printf("%d % %d = %d", a, b, a%b);
       break;
       default:
       printf("Invalid Operator!");
       break;
     }
return 0;
}

the problem is in line 14

After you enter an integer you press the Enter key, right? Well, scanf() doesn't remove that key, so it remains in the keyboard buffer.

When you enter a number, scanf() will ignore white space until it reaches the first numeric digit then stop reading the keyboard when it reaches a character that is not a diget.

When you enter a string, scanf() reads all the characters in the keyboard buffer until it reaches the first white space character (space, tab, carriage return, linefeed). So if you previously called scanf() to enter a number, such as "%d" and then call scanf() again with "%s", the first thing scanf() finds is a white space, so it will do nothing.

C-language has no good standard way to clear everything from the keyboard buffer. The easiest way to remove the '\n' from the keybaord buffer is to call getchar() after each call to scanf() with "%d".

Comments
Thanks-- the getchar() worked
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
char op;
printf("Welcome To Command Based Calculator\n");
printf("Now enter two numbers");
scanf("%d",&a);
scanf("%d",&b);
printf("Select the desired operator\n");
scanf(" %c",&op);
printf("the value in op is %c\n",op);
switch(op)
{
case '+':
printf("%d + %d = %d", a, b, a+b);
break;
case'-':
printf("%d - %d = %d", a, b, a-b);
break;
case'*':
printf("%d * %d = %d", a, b, a*b);
break;
case'/':
printf("%d / %d = %d", a, b, a/b);
break;
case'%':
printf("%d % %d = %d", a, b, a%b);
break;
default:
printf("Invalid Operator!");
break;
}
getch();
}

Don't know the exact problem but when you write %c near first double qoute that is "%c" then it takes some value other than what character you are going to enter.
In above code i have written scanf as scanf(" %c",&op); and it worked it's very unusual.

Mark Thread as solved if you have got the solution Thank You and "Keep Coding"

Edited 3 Years Ago by <Anurag>: updation

This question has already been answered. Start a new discussion instead.