+ 1

What's wrong with the following . Its simple calculator but not working correctly on computer as well as mobile.

#include<stdio.h> int main() { float a,b; char c; printf("Enter two numbers\n"); scanf("%f%f",&a,&b); printf("press + for addition - for subtraction * for multiplication or / for division\n"); scanf("%c",&c); switch(c) { case '+' : printf("%f + %f = %f",a,b,a+b); break; case '-': printf("%f - %f = %f",a,b,a-b); break; case '*' : printf("%f * %f = %f",a,b,a*b); break; case '/' : printf("%f / %f = %f",a,b,a/b); break; default : printf("Entered Wrong Choice"); } }

31st Mar 2022, 6:06 PM
Mohammad Mushahid Hussain
2 Answers
+ 4
Two options:- printf("Enter two numbers\n"); scanf("%f %f",&a,&b); fflush(stdin); // add this...or printf("press + for addition - for subtraction * for multiplication or / for division\n"); scanf(" %c",&c); // put a space just before the %c.
31st Mar 2022, 7:14 PM
rodwynnejones
rodwynnejones - avatar
0
Yes it works but please can you tell me why space is necessary before the %c.
1st Apr 2022, 3:39 AM
Mohammad Mushahid Hussain