Search code examples
cprintfcalculatorscanfgetchar

Trying to get a variable from getchar() to keep it's value in a simple calculation program


I'm learning C and I'm trying to create this program, that asks, in order, to enter an operator(+,-,*,%), a number then another number using scanf, printf and getchar(). When I do it asking the operator last it works, but I have to have it with the operator first.

Here is the code I've come up with that works (but is not in order):

char operator;
int numbers[2];
int result;

printf("Enter the first number:");
scanf("%9d", &numbers[0]);
printf("Enter the second number:");
scanf("%9d", &numbers[1]);
printf("Enter operation:");
getchar();
operator = getchar();
if (operator == '+') {
result = numbers[0] + numbers[1];
} 
else if (operator == '-') {
result = numbers[0] - numbers[1];
}
else if (operator == '*') {
result = numbers[0] * numbers[1];
}
else if (operator == '/') {
result = numbers[0] / numbers[1];
}
else {
result = 0;
}

printf("%d", result);

This is the code that doesn't:

printf("Enter operation:");
getchar();
char operator = getchar();
if (operator == '+') {
result = numbers[0] + numbers[1];
} 
else if (operator == '-') {
result = numbers[0] - numbers[1];
}
else if (operator == '*') {
result = numbers[0] * numbers[1];
}
else if (operator == '/') {
result = numbers[0] / numbers[1];
}
else {
result = 0;
}
printf("Enter the first number:");
scanf("%9d", &numbers[0]);
printf("Enter the second number:");
scanf("%9d", &numbers[1]);

printf("%d", result);

This one gives a random variable.

If the code wasn't in a block, forgive me, I followed the instructions as best I could.


Solution

  • The result of assignment operator is calculated "right at the moment": expression...

    result = a + b;
    

    ... will not make result change its value when a or b change their values.

    That's why your first snippet works as planned, and your second snippet shows garbage, as you first try to calculate the result of some binary operation on some garbage values (what numbers array elements store before they got assigned something useful), and only then try to get some input from user.

    The key is result already has some value to this point, and this value won't change - unless you make a second assignment, of course. )

    It's quite easy to fix this program: just separate the actual calculations from that input part. So that...

    if (operator == '+') {
      result = numbers[0] + numbers[1];
    } 
    else if (operator == '-') {
      result = numbers[0] - numbers[1];
    }
    else if (operator == '*') {
      result = numbers[0] * numbers[1];
    }
    else if (operator == '/') {
      result = numbers[0] / numbers[1];
    }
    else {
      result = 0;
    }
    

    ... will follow ALL the printf/scanf parts (except the one that prints the result, obviously). BTW, you can use switch for the same purpose:

    switch(operator) {
      case '+':
        result = numbers[0] + numbers[1]; break;
      case '-':
        result = numbers[0] - numbers[1]; break;
      case '*':
        result = numbers[0] * numbers[1]; break;
      case '/':
        result = numbers[0] / numbers[1]; break;
      default:
        result = 0;
    }