lundi 2 novembre 2020

"%c" is being recognized only if there is space in between " and %c [duplicate]

i was building a calculator which does *,+,-,/ , between 2 numbers, [so when the part to scan the operation(to be applied between 2 numbers) comes ,the program just skip's the part to scan and prints invalied input ] all the above happens if i use scanf("%c",&oper); in the program given below.

but the program is running without any hiccup if i use scanf(" %c",&oper);

double  X;
double Y;
char oper;
printf("enter the first number: ");
scanf("%lf",&X);

printf("enter the operation you want ,like + - * / :     ");
scanf("%c",&oper);
/*right here*/

printf("\nenter your second number:");
scanf("%lf",&Y);

if (oper=='+'){printf("%f",X + Y);}
else if (oper=='*'){printf("%f",X*Y);}
else if (oper=='-'){printf("%f",X-Y);}
else if (oper == '/') {printf("%f",X/Y);}
else {printf("invalied input");}

Aucun commentaire:

Enregistrer un commentaire