+ 1
What's wrong with this code? It's not asking for entering character. I'm unable to input any character.
#include <stdio.h> void main(){ float num1,num2; char ch; printf("Enter the numbers:"); scanf("%f %f", &num1, &num2); printf("Enter operation:"); scanf("%c", &ch); switch(ch){ case '+': printf("%f",num1+num2); break; case '-': printf("%f",num1-num2); break; case '*': printf("%f",num1*num2); break; case '/': printf("%f",num1/num2); break; } }
2 Respostas
+ 2
Add a space before the %c format specifier, as follows:
scanf (" %c", &ch);
What happens here is that we have a '\n' character in the input stream from previous input (when reading <num1> & <num2>). By adding the space before %c specifier the '\n' character it will work.
Good luck!
+ 1
Thanks a lot.....it worked