0

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


#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;
}

1
  • 1
    Try for example to enter abc instead of a number when prompted, and see what happens next. That's one error case that checking the return value of scanf would catch. Commented Aug 24, 2020 at 22:23

1 Answer 1

5

It is because return value is ignored.

You should check return values of scanf() to check if readings are successful.

#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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.