+ 1

if i want to take two numbers as input at first, why It's not work?

#include<stdio.h> int main () { double num1, num2; char oper; printf("Enter two numbers: "); scanf("%lf %lf", &num1, &num2); printf("Enter an opearator (+, -, *,/) : "); scanf("%c", &oper); switch(oper) { case '+': { printf("%lf + %lf = %lf\n", num1, num2, num1+num2); break; } case '-': { printf("%lf - %lf = %lf\n", num1, num2, num1-num2); break; } case '*': { printf("%lf * %lf = %lf\n", num1, num2, num1*num2); break; } case '/': { printf("%lf / %lf = %lf\n", num1, num2, num1/num2); break; } default: printf("Not a valid operator\n"); } return 0; }

4th May 2020, 5:34 PM
Fahad
Fahad - avatar
1 ответ
+ 4
scanf("%c", &oper); Is where your problem lies. This is due to the remaining newline character left over from entering the numbers. You can resolve it by simply adding a space prior to the %c so that it consumes the leading whitespace characters. scanf(" %c", &oper); https://stackoverflow.com/questions/13542055/how-to-do-scanf-for-single-char-in-c
4th May 2020, 5:46 PM
ChaoticDawg
ChaoticDawg - avatar