0
#include <stdio.h>
int calculator(int num1,int num2, char p)
{
    switch(p)
    {
        case '+':
            return num1+num2;
        case '-':
            return num1-num2;
        case '*':
            return num1*num2;
        case '/':
            return num1/num2;
        default:
            printf("Error\n");
            break;
    }
}

int main()
{
    int num1,num2,answer=0;
    char p;
    while(1)
    {
        answer=0;
        printf("Enter 2 numbers\n");
        scanf("%d%d",&num1,&num2);
        printf("Enter the operator\n");
        scanf(" %c",&p);
        answer=calculator(num1,num2,p);
        printf("the answer is %d\n",answer);
    }
    return 0;
}

I get 6 every time the default statement is executed no matter the input this is my input ( Enter 2 numbers first number 4 second number 8 Enter the operator operator H ) the output ( Error the answer is 6 )

  • 1
    Since there's no `return` statement in the default case, or at the end of the function, you're picking up random garbage from the return value register. Add a `return` statement. – Tom Karzes Jun 15 '23 at 08:18
  • 3
    If you are compiling with gcc or clang, be sure to compile with options `-Wall -Wextra -Werror`. Then you won't have this problem. – user3386109 Jun 15 '23 at 08:22
  • BTW, it is not a *total* coincidence that you get the value 6 after printing 6 characters. See [printf return value](https://en.cppreference.com/w/c/io/fprintf#Return_value) – BoP Jun 15 '23 at 08:28
  • What value do you expect?? – ikegami Jun 15 '23 at 13:48

2 Answers2

4

Your calculator function doesn't execute a return for the break fallthrough case. Strictly speaking this is undefined behavior. See Why and how does GCC compile a function with a missing return statement?

datenwolf
  • 159,371
  • 13
  • 185
  • 298
1

It is undefined behaviour as you do not return anything in your default: case. Also you need to let the caller that your function failed. You can do this by setting a sentinel value or introducing another parameter which will indicate the error.

#include <stdio.h>
int calculator(int num1,int num2, char p, int *error)
{
    int result = 0;
    *error = 0;

    switch(p)
    {
        case '+':
            result = num1+num2;
            break;
        case '-':
            result = num1-num2;
            break;
        case '*':
            result = num1*num2;
            break;
        case '/':
            result = num1/num2;
            break;
        default:
            *error = 1;
            break;
    }
    return result;
}

int main(void)
{
    int num1,num2,answer=0, error;
    char p;
    while(1)
    {
        answer=0;
        printf("Enter 2 numbers\n");
        if(scanf("%d %d",&num1,&num2) != 2)
        {
            printf("you have entered wrong numbers\n");
            continue;
        }
        printf("Enter the operator\n");
        if(scanf(" %c",&p) != 1) { /* handle arror */};
        answer=calculator(num1,num2,p, &error);
        if(error)
            printf("Error\n");
        else
            printf("the answer is %d\n",answer);
    }
    return 0;
}

You also need to check if the scanf did not fail.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • If you are talking about error checking, when a division is made, check `if ( num2==0 || ( -INT_MAX>INT_MIN && num1==INT_MIN && num2==-1 ) ) { *error=1; break; }`. This is to avoid division by 0 and, if the system uses 2-complement, check that the division doesn't create a overflow. And also check for overflow / underflow in the other operants. – 12431234123412341234123 Jun 15 '23 at 09:00
  • @12431234123412341234123 he can improve his code. I am **only showing** the idea not writing the calculator for him. SO works like this. But you are free to write fully functional calculator and post it as an answer – 0___________ Jun 15 '23 at 09:02
  • Yes, but mentioning it is better. Most people don't understand that a division by `-1` can be a problem, and some don't know that they have to check the input values. – 12431234123412341234123 Jun 15 '23 at 09:04