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

Recommended Answers

All 2 Replies

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".

commented: Thanks-- the getchar() worked +0
#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"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.