ZueJameAA
ZueJameAA

Reputation: 37

How to get the right output on c

These code are running for calculate someone's height and weight to know if he is too fat or not.

But as I run by anything I input, they all output "too fat" does it have any problems.

#include <stdio.h>

int main() {
    double height, weight, standardweight;
    int n, i;

    scanf("%d", &n);

    for (i = 1; i <= n; i++)
    {
        scanf("%d %d", &height, &weight);
        standardweight = (height - 100) * 0.9;

        if (weight * 0.5 - standardweight >= standardweight * 0.1)
            printf("You are too fat!\n");
        else if (standardweight - weight * 0.5 <= standardweight * 0.1)
            printf("You are too thin!\n");
        else
            printf("You are wan mei!\n");
    }
    return 0;
}

Upvotes: 0

Views: 88

Answers (1)

alenada99
alenada99

Reputation: 356

I see you are trying to read integers values using the scanf function while you should read double values (%d is for int values).

Change to %lf to read double values:

scanf("%lf %lf", &height, &weight);

You should also check the scanf return value to see if the function failed (it returns the number of inputs successfully assigned):

if(scanf("%lf %lf",&height, &weight) != 2) {
    fprintf(stderr, "scanf fail!\n");
    exit(EXIT_FAILURE);
}

It is very strange that your compiler did not warn you about the error, if you missed it pay close attention about the result of your compilation.

I would also suggest to use some printfs when you encounter errors. For example you should have printed the given inputs:

printf("height: %lf, weight: %lf\n", height, weight);

Upvotes: 1

Related Questions