#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 )
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?