[英]Error using scanf: return value ignored with floating point
this is my code and I'm not sure why I'm getting an error whenever I try to test it.这是我的代码,我不确定为什么每次尝试测试时都会出错。 It keeps saying return value ignored with scanf
它一直说 scanf 忽略了返回值
#include <stdio.h>
#include <math.h>
int main(void) {
float money, tax, result;
printf("Enter the amount of money.");
scanf("%f", &money);
tax = 0.05 * money;
result = tax + money;
printf("With tax added: $%f", result);
return 0;
}
It is because return value is ignored.这是因为忽略了返回值。
You should check return values of scanf()
to check if readings are successful.您应该检查
scanf()
返回值以检查读数是否成功。
#include <stdio.h>
#include <math.h>
int main(void) {
float money, tax, result;
printf("Enter the amount of money.");
if (scanf("%f", &money) != 1) {
fputs("read error!\n", stderr);
return 1;
}
tax = 0.05 * money;
result = tax + money;
printf("With tax added: $%f", result);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.