我的身高和长度不保存我给的价值
My height and length doesn't save the value that i give
每次打印高度或长度时,值为0。就像C不保存xHeight、yHeight、xLength或yLength的值一样。我检查了一下,我认为不是语法问题。
#include <stdio.h>
#include <stdlib.h>
int main()
{
float xHeight,yHeight,xLength,yLength, height = 0.0, length = 0.0;
printf("Tell me the x and y: \n");
scanf(" %.2lf", &xHeight);
fflush(stdin);
scanf("%.2lf",&yHeight);
fflush(stdin);
height = xHeight - yHeight;;
printf("\n%.2lf",height);
printf("Now tell me another x and y: \n");
scanf(" %.2lf", &xLength);
fflush(stdin);
scanf("%.2lf",&yLength);
fflush(stdin);
length = xLength - yLength;
printf("\n%.2lf", length);
printf("\n==========================================\n");
printf("The perimeter of this rectangle is %.2lf", 2*(length+height));
return 0;
}
%.2lf
不是 float
的正确转换规范。应该没有.2
,因为scanf
不使用精度数,应该是f
而不是lf
,因为f
是指向指针的float
而 lf
是指向 double
.
的指针
在 scanf
中 %f
之前也不需要 space 字符; %f
转换自动跳过初始白色 space.
使用 scanf("%f", &xHeight);
并在其他 scanf
调用中使用类似方法。
每次打印高度或长度时,值为0。就像C不保存xHeight、yHeight、xLength或yLength的值一样。我检查了一下,我认为不是语法问题。
#include <stdio.h>
#include <stdlib.h>
int main()
{
float xHeight,yHeight,xLength,yLength, height = 0.0, length = 0.0;
printf("Tell me the x and y: \n");
scanf(" %.2lf", &xHeight);
fflush(stdin);
scanf("%.2lf",&yHeight);
fflush(stdin);
height = xHeight - yHeight;;
printf("\n%.2lf",height);
printf("Now tell me another x and y: \n");
scanf(" %.2lf", &xLength);
fflush(stdin);
scanf("%.2lf",&yLength);
fflush(stdin);
length = xLength - yLength;
printf("\n%.2lf", length);
printf("\n==========================================\n");
printf("The perimeter of this rectangle is %.2lf", 2*(length+height));
return 0;
}
%.2lf
不是 float
的正确转换规范。应该没有.2
,因为scanf
不使用精度数,应该是f
而不是lf
,因为f
是指向指针的float
而 lf
是指向 double
.
在 scanf
中 %f
之前也不需要 space 字符; %f
转换自动跳过初始白色 space.
使用 scanf("%f", &xHeight);
并在其他 scanf
调用中使用类似方法。