As you can see this code work fine when I ask for char input first It work without any problem.As you can see this code work fine when I ask for char input first It work without any problem.As you can see this code work fine when I ask for char input first It work without any problem.
#include<stdio.h>
int main(){
char mathoperation;
int num1,num2;
printf("Give the math operation you want to use : ");
scanf("%c",&mathoperation);
printf("Give the first number of you opperation : ");
scanf("%d",&num1);
printf("Give the second number of you opperation : ");
scanf("%d",&num2);
switch (mathoperation)
{
case '+':
printf("%d\n",num1+num2);
break;
case '-':
printf("%d\n",num1-num2);
break;
case '*':
printf("%d\n",num1*num2);
break;
case '/':
if(num2==0)
printf("No you cant do that");
else
printf("%d\n",num1/num2);
break;
case '%':
printf("%d\n",num1%num2);
break;
default:
printf("Error!");
break;
}
return 0;
}
But when I ask for input for the integer value first it does not ask input for char. It directly go to defaut and print error.But when I ask for input for the integer value first it does not ask input for char. It directly go to defaut and print error.But when I ask for input for the integer value first it does not ask input for char. It directly go to defaut and print error.But when I ask for input for the integer value first it does not ask input for char. It directly go to defaut and print error.But when I ask for input for the integer value first it does not ask input for char. It directly go to defaut and print error.
#include<stdio.h>
int main(){
char mathoperation;
int num1,num2;
printf("Give the math operation you want to use : ");
scanf("%c",&mathoperation);
printf("Give the first number of you opperation : ");
scanf("%d",&num1);
printf("Give the second number of you opperation : ");
scanf("%d",&num2);
switch (mathoperation)
{
case '+':
printf("%d\n",num1+num2);
break;
case '-':
printf("%d\n",num1-num2);
break;
case '*':
printf("%d\n",num1*num2);
break;
case '/':
if(num2==0)
printf("No you cant do that");
else
printf("%d\n",num1/num2);
break;
case '%':
printf("%d\n",num1%num2);
break;
default:
printf("Error!");
break;
}
return 0;
}