0

I have tried to write my first calculator, and found some examples online, which I then changed to make them easier in terms of flow. However when I change the flow from this:

#include <stdio.h>

main()
{
    char operator;
    float num1,num2;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c" ,&operator);
    printf("Enter first operand: ");
    scanf("%f" ,&num1);
    printf("Enter second operand: ");
    scanf("%f" ,&num2); 

    switch(operator)
    {
        case '+':
            printf("num1+num2=%.2f\n" ,num1+num2);
            break;
        case '-':
            printf("num1-num2=%.2f\n" ,num1-num2);
            break;
        case '*':
            printf("num1*num2=%.2f\n" ,num1*num2);
            break;
        case '/':
            printf("num1/num2=%.2f\n" ,num1/num2);
            break;
        default: //of operator is other than +, -, *, /, erros message shown
        printf("Error! Invalid operator, this is basic math only.\n");
    }       
    return 0;
}

to this:

#include <stdio.h>

main()
{
    char operator;
    float num1,num2;


    printf("Enter first operand: ");
    scanf("%f" ,&num1);
    printf("Enter an operator (+, -, *, /): ");
    scanf("%c" ,&operator);
    printf("Enter second operand: ");
    scanf("%f" ,&num2); 

    switch(operator)
    {
        case '+':
            printf("num1+num2=%.2f\n" ,num1+num2);
            break;
        case '-':
            printf("num1-num2=%.2f\n" ,num1-num2);
            break;
        case '*':
            printf("num1*num2=%.2f\n" ,num1*num2);
            break;
        case '/':
            printf("num1/num2=%.2f\n" ,num1/num2);
            break;
        default: //of operator is other than +, -, *, /, erros message shown
        printf("Error! Invalid operator, this is basic math only.\n");
    }       
    return 0;
}

basically changed the flow from: enter operator, then enter first number, then second number. To: enter first number, then enter operator, then enter second number. My problem is when I do this, I see the Enter operator, but the program skips over the option to enter the operator and asks for: enter first number then enter second number. The response is the default switch.

General Grievance
  • 4,555
  • 31
  • 31
  • 45

3 Answers3

0

The newline you enter after scanf reads in your first operator is being taken by the second scanf calls. See this question for a more detailed explanation.

In short, write a function like this, and call it after every scanf call.

void clear_stdin(void)
{
    while(getchar() != '\n');
}
Community
  • 1
  • 1
charmlessCoin
  • 754
  • 3
  • 13
0

it's because the newline character stays in the buffer when you enter the input of the first scanf , so the following scanf receive it as it's input , just put a getchar() after every scanf() that'll solve it

Farouq Jouti
  • 1,657
  • 9
  • 15
0

New-line left in input buffer.

When using scanf("%f",..., the %f consume leading white-space, but no trailing white-spaces after the number - usually the \n.

When using scanf("%c",..., the %c does not consume leading white-space, and no trailing white-spaces after the char either.

To consume leftover white space (e.g. the \n from the previous scanf()), simple precede %c with a space.

// scanf("%c" ,&operator);
scanf(" %c" ,&operator);  // add space.
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • you are right this worked. what is the reason for this phenomenon? Is this an old bug in the c language or was this done purposefully and with a reason? – Max Grossenbacher Sep 22 '13 at 17:16
  • Assuming "this" in "Is this an old bug" refers to `%c` consuming whitespace: It is not a bug. `%c` scans in 1 `char`, _any_ 1 `char`, no exceptions, including `' '`, `'\n'`, etc. and even `'\0'` (rarely encountered in reading text.) To avoid scanning in whitespace, pre-pending the `' '` consumes all whitespace before `%c`. Thus `" %c"` will not store a white space. `%c` allows one to use `scanf()` to scan into a single `char` a space character. – chux - Reinstate Monica Sep 22 '13 at 20:09