I will
guess that you have a whitespace problem. See this write-up on
scanf[
^]. That's my go-to site when I have detailed questions about C++. It's also great for C, which I don't use. Note the example part way down, which looks very much like yours:
scanf("%d", &a);
scanf(" %c", &c);
Note the space at the beginning of their
" %c"
. This suggests that there was still whitespace in the buffer after you read the second integer, and that your first call to
scanf
returned it (probably a
'\n'
) before the second one read the character that you entered.
You would have figured this out if you had checked if
c
was a
'+'
,
'-'
, or
something else.
scanf
also returns an
int
, which indicates whether it succeeded, and this is also something that you should check. Simply
assuming that your code works leads to this kind of bewilderment. When you work on large programs that need to be resilient, as much as 90% of the code involves checking for, and handling, error situations.