1

This is a very simple program I made to test some stuff with structs which I'm new with.

If you try to execute the program, you'll notice that the first if in the void function is ignored (or maybe not satisfied) even when 19 <= BMI <= 25

#include <stdio.h>
#include <string.h>
#include <ctype.h>
typedef struct {
        char name[50];
        float weigth;
        float heigth;
        float BMI;
} subjects;
void check_BMI(subjects Candidate)
{
        if (Candidate.BMI >= 19 && Candidate.BMI <= 25)
                puts("Subject is probably in good shape");
        else
                puts("Subject might not be in good shape");
}
int main()
{
        subjects Candidate;
        Candidate.BMI = Candidate.weigth/(Candidate.heigth * Candidate.heigth);
        puts("Insert subject's name: ");
        fgets(Candidate.name, 50, stdin);
        puts("Insert subject's weigth (in kg): ");
        scanf("%f", &Candidate.weigth);
        puts("Insert subject's heigth (in m): ");
        scanf("%f", &Candidate.heigth);
        check_BMI(Candidate);
        return 0;
}

What could be my mistake?

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Giorgio DT
  • 71
  • 3
  • 2
    `Candidate.BMI = Candidate.weigth/(Candidate.heigth * Candidate.heigth);` move to after input (before `check_BMI(Candidate);`). – BLUEPIXY Jun 22 '15 at 10:48
  • Please include the code in the question. – Tilman Hausherr Jun 22 '15 at 10:49
  • 1
    Do you actually calculate the BMI *BEFORE* you fetch the values from the input? :D – Nitram Jun 22 '15 at 10:53
  • 1
    @Nitram: some programming languages work that way: [What is the difference between declarative and imperative programming](http://stackoverflow.com/q/1784664/2564301). But C does not. – Jongware Jun 22 '15 at 11:42

2 Answers2

1

You're doing it in the wrong order:

    Candidate.BMI = Candidate.weigth/(Candidate.heigth * Candidate.heigth);
    puts("Insert subject's name: ");
    fgets(Candidate.name, 50, stdin);
    puts("Insert subject's weigth (in kg): ");
    scanf("%f", &Candidate.weigth);
    puts("Insert subject's heigth (in m): ");

You can't calculate the BMI as ratio of two numbers that haven't yet been entered. How is that supposed to work?

Move the BMI calculating line to a place after you've entered all numbers, and before you print the result.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
1

You have

subjects Candidate;

which is uninitialized. Right after the declaration, you use

Candidate.BMI = Candidate.weigth/(Candidate.heigth * Candidate.heigth);

which calculates Canditate.BMI with the uninitialized values (garbage:Candidate.weigth , Candidate.heigth). This leads to Undefined Behavior.

Fix the problem by moving

Candidate.BMI = Candidate.weigth/(Candidate.heigth * Candidate.heigth);

just before

check_BMI(Candidate);

so that the values are initialized when calculating Canditate.BMI.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • Thanks, how stupid am I? I've got an exam in about 30 minutes and I can't do stuff properly. It was a stupid mistake, got it, thanks. – Giorgio DT Jun 22 '15 at 10:53
  • @GiorgioDT , Never mind. It happens to all of us occasionally... And you're welcome! :-) – Spikatrix Jun 22 '15 at 11:00