英文:
why do i get 6 from default statement if my input was another character other than (+, -, *, /)
问题
我每次执行默认语句时都会得到6,无论输入是什么
这是我的输入
(
输入2个数字
第一个数字 4
第二个数字 8
输入操作符
操作符 H
)
输出结果
(
错误
答案是 6
)
英文:
#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
得分: 4
你的 calculator 函数在 break 的情况下没有执行 return。严格来说,这是未定义行为。请参考 https://stackoverflow.com/questions/7280877/why-and-how-does-gcc-compile-a-function-with-a-missing-return-statement
英文:
Your calculator function doesn't execute a return for the break fallthrough case. Strictly speaking this is undefined behavior. See https://stackoverflow.com/questions/7280877/why-and-how-does-gcc-compile-a-function-with-a-missing-return-statement
答案2
得分: 1
以下是您要翻译的代码部分:
这是因为在您的 `default:` 情况下没有返回任何内容,它是未定义的行为。您还需要通知调用者函数失败了。您可以通过设置 [哨兵值][1] 或引入另一个参数来指示错误来实现这一点。
#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("输入2个数字\n");
        if (scanf("%d %d", &num1, &num2) != 2)
        {
            printf("您输入了错误的数字\n");
            continue;
        }
        printf("输入操作符\n");
        if (scanf(" %c", &p) != 1) { /* 处理错误 */ };
        answer = calculator(num1, num2, p, &error);
        if (error)
            printf("错误\n");
        else
            printf("答案是 %d\n", answer);
    }
    return 0;
}
您还需要检查 `scanf` 是否失败。
[1]: https://en.wikipedia.org/wiki/Sentinel_value
请注意,这是代码的翻译版本,没有其他内容。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论