why do i get 6 from default statement if my input was another character other than (+, -, *, /)

huangapple go评论105阅读模式
英文:

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 &lt;stdio.h&gt;
int calculator(int num1,int num2, char p, int *error)
{
int result = 0;
*error = 0;
switch(p)
{
case &#39;+&#39;:
result = num1+num2;
break;
case &#39;-&#39;:
result = num1-num2;
break;
case &#39;*&#39;:
result = num1*num2;
break;
case &#39;/&#39;:
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(&quot;Enter 2 numbers\n&quot;);
if(scanf(&quot;%d %d&quot;,&amp;num1,&amp;num2) != 2)
{
printf(&quot;you have entered wrong numbers\n&quot;);
continue;
}
printf(&quot;Enter the operator\n&quot;);
if(scanf(&quot; %c&quot;,&amp;p) != 1) { /* handle arror */};
answer=calculator(num1,num2,p, &amp;error);
if(error)
printf(&quot;Error\n&quot;);
else
printf(&quot;the answer is %d\n&quot;,answer);
}
return 0;
}

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

huangapple
  • 本文由 发表于 2023年6月15日 16:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定