What's wrong with this code? It's not asking for entering character. I'm unable to input any character. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; } }

24th Aug 2019, 1:12 PM
Kazi Md. Shahriar
Kazi Md. Shahriar - avatar
2 Answers
+ 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!
24th Aug 2019, 1:30 PM
Ipang
+ 1
Thanks a lot.....it worked
24th Aug 2019, 3:42 PM
Kazi Md. Shahriar
Kazi Md. Shahriar - avatar