【发布时间】:2017-08-11 11:14:21
【问题描述】:
我刚刚开始学习 c 编程。
对于这个问题,我编写了以下代码。你能帮我找出错误吗?我没有得到想要的结果,最后一个 else 中的语句总是被执行。
#include<stdio.h>
#include<conio.h>
void dummy(float *a)
{
float b=*a; //perform some floating access
dummy (&b); //calling a floating point function
}
void main()
{
double x,y;
clrscr();
scanf("%lf %lf",x,y);
if(x==0 && y!=0)
{
printf("The point lies on the y-axis.");
}
else if(y==0 && x!=0 )
{
printf("The point lies on the x-axis.");
}
else if(x==0 && y==0)
{
printf("The point is the origin");
}
else
{
printf("The point lies neither on the x nor the y axis ");
}
getch();
}
【问题讨论】:
-
请编辑您的问题以包括导致错误行为的输入,以及实际和预期的输出。
-
嗨。无论我输入的 2 个数字是什么,语句“该点既不在 x 也不在 y 轴上”都会被执行
-
这就是你不检查scanf的返回值时必须发生的事情。
-
scanf 的返回值?
-
永远不要直接比较浮点值以获得(不)相等性:就像这里所说的那样:stackoverflow.com/a/4858547/8051589。这可能是问题所在。
标签: c