1
#include <stdio.h>
#include <stdlib.h>

int main()
{

    int num1;
    int num2;
    char op;

    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter an operator: ");
    scanf("%c", &op);
    printf("Enter the second number: ");
    scanf("%d", &num2);

    switch(op){

        case'+':
            printf("%d", num1+num2);
            break;

        case'-':
            printf("%d", num1-num2);
            break;

        case'/':
            printf("%d", num1/num2);
            break;

        case'*':
            printf("%d", num1*num2);
            break;

        default:
            printf("Enter a valid Operator");

    }

    return 0;
}

I tried to build a basic calculator with user input. but I am getting an error in this line scanf("%c", &op); I searched in here(Stackoverflow) and I also found the answer that if I put a space in scanf(" %c", &op) then my program will work fine; now the question I have is, Could someone explain me this in laymen's terms for a beginner? Please. Your answer will be much appreciated

Ajo Mathew
  • 101
  • 7
  • Conider: `printf("Enter the expression (eg 6 * 7): "); fflush(stdout); char expression[1000]; if (fgets(expression, sizeof expression, stdin)) { /* parse expression and show result/error */ }` – pmg Nov 07 '21 at 12:41
  • 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) – alex01011 Nov 07 '21 at 13:00

4 Answers4

0

Prepend the conversion specifier in the format string with a space like

scanf( " %c", &op );
       ^^^^^  

In this case white space characters in the input stream as the new line character '\n' that corresponds to the pressed key Enter will be skipped

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

scanf manual:

specifier c:

Matches a sequence of characters whose length is specified by the maximum field width (default 1); the next pointer must be a pointer to char, and there must be enough room for all the characters (no terminating null byte is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.

ie format scanf(" %c", &op).

After typing the first number for int num1 you type an enter '\n' the next scan for character captures the new line and prints it . So as per the manual, To skip white space first, use an explicit space in the format:

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

or use like this below:

printf("Enter an operator: ");
scanf("%c", &op);
scanf("%c", &op);
Ajo Mathew
  • 101
  • 7
0

The problem is not with scanf but stdin. stdin and stdout refer to the same file in the memory for console application. So there is '\n' in the stdin which you have entered for first scanf which is taken by scanf and stored in op. Try putting scanf("%c", &op); above scanf("%d", &num1); or write fflush(stdin) above scanf("%c", &op);

frippe
  • 1,329
  • 1
  • 8
  • 15
  • `fflush(stdin)` is generally a bad idea. – alex01011 Nov 07 '21 at 12:55
  • Why is it a bad idea? I am just curious. – Tauqeer Akhtar Nov 07 '21 at 12:58
  • `fflush` is meant to be used on output streams. For reference [Using fflush(stdin)](https://stackoverflow.com/questions/2979209/using-fflushstdin) – alex01011 Nov 07 '21 at 12:59
  • Now I understand the lack of portability of fflush(stdin) , Thank you. – Tauqeer Akhtar Nov 07 '21 at 13:05
  • I don't see how `stdout` is relevant here. Yes, `stdin`, `stdout` and `stderr` all point to the same file descriptor by default when run from a terminal (at least on Linux, can't say for Windows), but that's irrelevant for the question. The `'\n'` is left in `stdin` since `scanf` doesn't consume nor discard the newline character originating from the user confirming the input with the enter key. – frippe Nov 07 '21 at 17:46
  • yes it is all about `stdin` . That is extra bit of information , may be helpful in future. – Tauqeer Akhtar Nov 07 '21 at 17:51
-3

Try using 'getc' and 'gets' instead. 'scanf' is considered to be unsafe altogether and it would be wise to search for safer alternatives. That way you will have greater control over user input.

MLquest
  • 19
  • 6
  • `gets()` is no more a standard and it might lead to buffer overflow so you should use `fgets()` in-order to read till end of line . In order to read char by char until you encounter space you can use `getc()`. – vegan_meat Nov 07 '21 at 12:52
  • 4
    Not sure if you are joking or not. Talking about scanf being dangerous and recommending gets instead? gets is not part of C anymore, it was removed because it was to dangerous to use. – Fredrik Nov 07 '21 at 12:54