c - "assignment makes integer from pointer without a cast -wint-conversion"

标签 c syntax-error

我是c语言新手,我想制作一个计算器,所以我写了这段代码:

#include<stdio.h>
void main()
{
    int x,y,z;
    char m;
    printf("enter the first number");
    scanf("%d",&x);
    printf("enter the second number");
    scanf("%d",&y);
    printf("enter the math operator");
    scanf("%c",&m);
if (m == '+')
{
    z=x+y;
    printf("the answer is %d",z);

}
else if(m == '-')
{
    z=x-y;
    printf("the answer is %d",z);
}
else
{
    printf("wrong symbol");
}
    }

当我运行程序时它会忽略

scanf("%c",&m);

if (m == '+')
{
    z=x+y;
    printf("the answer is %d",z);

}
else if(m == '-')
{
    z=x-y;
    printf("the answer is %d",z);
}

它会直接转到

else
    {
        printf("wrong symbol");
    }

它显示如下:https://i.imgur.com/QQF8ftg.jpg 请帮忙。

最佳答案

您只需要刷新流条目标准输入,因为当您调用 scanf("%c",&m); 时,某些字符仍在标准输入中。由于它只查找一个字符...它直接获取标准输入中已有的第一个字符('\n')。

在调用 scanf 之前插入此可移植代码以刷新标准输入:

int c;
while((c = getchar()) != '\n' && c != EOF);

希望这有帮助!

关于c - "assignment makes integer from pointer without a cast -wint-conversion",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48830947/

相关文章:

MySQL 错误代码 : 1064. 您的 SQL 语法有误

php - PHP MVC在本地工作,但是当上传到主机时失败

c - 段错误 11,使用指针并返回它们的问题

c++ - 从应用程序中使用提升的权限调用 win32 服务中的方法

c - 8 张皇后对角线检查

php - GD库中的images()在类里面不起作用,但可以单独使用

php - 插入MySQL "on duplicate key update"语法错误

c - 覆盖到c中的特定行

c - 在格式化的 printf 中使用 #define 来定义精度

vba - 查询表达式 '2020-12-01'中的语法错误(缺少运算符)