简体   繁体   English

使用 scanf 时出错:返回值被浮点忽略

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM