#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1;
int num2;
char op;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter an operator: ");
scanf("%c", &op);
printf("Enter the second number: ");
scanf("%d", &num2);
switch(op){
case'+':
printf("%d", num1+num2);
break;
case'-':
printf("%d", num1-num2);
break;
case'/':
printf("%d", num1/num2);
break;
case'*':
printf("%d", num1*num2);
break;
default:
printf("Enter a valid Operator");
}
return 0;
}
I tried to build a basic calculator with user input. but I am getting an error in this line scanf("%c", &op);
I searched in here(Stackoverflow) and I also found the answer that if I put a space in scanf(" %c", &op)
then my program will work fine;
now the question I have is, Could someone explain me this in laymen's terms for a beginner? Please. Your answer will be much appreciated