0

int main()
{
  char Operator;

  int num1, num2, result;
 /* printf("Enter operator: ");
  scanf("%c", &Operator);*/

  printf("Enter first and second value: ");
  scanf("%d %d", &num1, &num2);

  printf("Enter operator: ");
  scanf("%c", &Operator);

  if(Operator == '+')
  {
    result = num1 + num2;
    printf("%d\n", result);
  }
  else if(Operator == '-')
  {
    result = num1 - num2;
    printf("%d\n", result);
  }

  return 0;
}

I tried making a simple calculator, and I put scanf(the one that requests an Operator) where it is now, and it does not work. But if I put it where the comment is, above the "Enter first and second value" it then works. I would just like to know why does it work on one place and not the other. Thanks in advance.

Baja
  • 55
  • 1
  • 1
  • 4
  • 1
    Its probably because of the input you are giving it suppose you enter `1 2 +` then num1 is 1, num2 is 2 and character that operator variable is going to have is space – Beshambher Chaukhwan May 19 '21 at 10:59
  • 4
    Does this answer your question? [problem in scanning character in C](https://stackoverflow.com/questions/5109512/problem-in-scanning-character-in-c) – Beshambher Chaukhwan May 19 '21 at 11:01
  • 1
    Does this answer your question? [scanf() leaves the new line char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) – Krishna Kanth Yenumula May 19 '21 at 11:03
  • 1
    I don't recommend that you use `scanf` for line-based input. Instead, I would recommend using `fgets` and `strtol`. See this page for further information: [A beginners' guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) – Andreas Wenzel May 19 '21 at 11:04
  • 1
    Just as a side note: It is unsafe to use `scanf` without checking the return value. See this page for further information: [A beginners' guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) – Andreas Wenzel May 19 '21 at 11:06

2 Answers2

2

As pointed out in comments, Your first scanf is reading the integer values but it's not reading the newline in buffer. When you do scanf("%c", &Operator) again for taking character input, it reads that newline character still present in input buffer. To avoid this, you can use scanf(" %c", %Operator) instead which will discard all whitespace characters before matching character:

printf("Enter operator: ");
scanf(" %c", &Operator);  

You can also use fgets() to read input into a string and then use sscanf() to read integers from string:

char input[100];

printf("Enter first and second value: ");
if (fgets(input, 100, stdin) == NULL) {
    printf("No input provided!\n");
    return -1;
}
if (strchr(input, '\n') == NULL) {
    printf("Input Buffer Overflow\n");
    return -1;
}
if (sscanf(input, "%d %d", &num1, &num2) != 2) {
    printf("Error in reading characters\n");
    return -1;
}

printf("Enter operator: ");
if (fgets(input, 100, stdin) == NULL) {
    printf("No input provided!\n");
    return -1;
}
if (strchr(input, '\n') == NULL) {
    printf("Input Buffer Overflow\n");
    return -1;
}
if (sscanf(input, "%c", &Operator) != 1) {
    printf("Error in reading characters\n");
    return -1;
}
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
  • 1
    Thanks, it looks a bit cleaner this way too. – Rohan Kumar May 19 '21 at 12:16
  • 1
    If you want perfect input validation, you may want to use `if ( strchr( input, '\n' ) == NULL)` to verify that the newline character was read, i.e. that the input line was not too long for the buffer size. On the hand, if your code focuses too much on input validation, then the essence of your answer may become less clear. – Andreas Wenzel May 19 '21 at 12:35
2

From the Open Group Base Specifications Issue,

Input white-space characters (as specified by isspace()) shall be skipped, unless the conversion specification includes a [, c, C, or n conversion specifier.

  1. %d format specifier will skip preceding input newline character (\n). That's why it works in the first case (case, where operator was read first) .

  2. scanf leaves an newline character \n in the input buffer, after the first scanf.

  3. The second scanf will read the extra \n from the input buffer to variable Operator. That's why it will not work.

  4. Since the format specifier is %c, the newline character will not be skipped.

  5. Solution : Use scanf(" %c", &Operator);. Add an extra space at the beginning.

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
  • Note that the question is not tagged POSIX. Therefore, the question is about ISO C and not POSIX C. For this reason, it seems more appropriate to quote [§7.21.6.2 ¶8 of the ISO C standard](http://port70.net/~nsz/c/c11/n1570.html#7.21.6.2p8) instead of the POSIX standard. – Andreas Wenzel May 19 '21 at 11:34