0
printf("\nInput first value and press ENTER : ");
scanf("%f", &num1);
char op;
printf("\nInput operation to perform.\nValid inputs are (+,-,*,/,**) Operation symbol =");
scanf("%c",&op);
float num2;
printf("\n\nInput second value and press ENTER: ");

The above is the snippet of code where the issue arises...

When Executed, it waits to collect num1, then it asks me what operation i want to perform but without an input from me, skips to ' Input second value and press ENTER: '... I'm new to C (started a week back) so help me out !

Alan Birtles
  • 27,579
  • 4
  • 25
  • 50
An Ant
  • 172
  • 1
  • 8
  • 1
    `scanf("%c",&op);`-->`scanf(" %c",&op);` – kiran Biradar Jun 15 '20 at 07:06
  • @kiranBiradar Okay i'll see, it should work... What is the logic behind it, though ? What did i miss ? – An Ant Jun 15 '20 at 07:09
  • 1
    The format specifiers `%d` and `%s` and `%f` automatically filter leading whitespace, but `%c` and `%[...]` and `%n` do not. You can instruct `scanf` to do so by adding a space just before the `%`. The `scanf` conversion stops at the first character it cannot convert, which is typically (but not necessarily) a space or a newline, and that character remains in the input buffer. – Weather Vane Jun 15 '20 at 07:10
  • So , basically, i should add a space after everytime i invoke %c or %n in scanf() ... – An Ant Jun 15 '20 at 07:15

0 Answers0