我是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/