dimanche 15 novembre 2020

Problem with BMI calculation program - if else and calculation errors in dev C++

For some reason, it doesn't let me insert the weight data, prints only the ''short man'' category (regardless of height data inserted), doesn't print BMI'S value and completely ignores the last ''if else'' statement. I can't seem to find the problem as it looks perfectly fine to me. I'm using dev c++.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char gender;
    float height;
    float weight;
    int category;
    float BMI;
    
    printf(" Enter gender: ");
    scanf("%c", &gender);
    printf(" Enter height: ");
    scanf(" %.2f", &height);
    printf(" Enter weight: \n");
    scanf(" %.1f", &weight);
           
    
    if ((gender == 'M'|| gender == 'm') && (height < 1.70 ))
    {
            category = 1;
            printf("Short man\n");
     }
    else if ((gender == 'M'|| gender == 'm') && (height >= 1.70 && height< 1.85))
         {
             category = 2;
             printf("Average man\n");
         }
    else if ((gender == 'M'|| gender == 'm') && (height >= 1.85))
         {
            category = 3;
            printf("Tall man\n");
         }
     
     else if (( gender == 'F' || gender == 'f' ) && (height < 1.60))
     {
              category = 4;
              printf("Short woman\n");
          }
    else if (( gender == 'F' || gender == 'f' ) && (height >= 1.60 && height <1.75))
          {
              category = 5;
              printf("Average woman\n");
          }
    else if (( gender == 'F' || gender == 'f' ) && (height >= 1.70))
          { 
            category = 6;
            printf("Tall woman\n");
          }
    
     
     switch(category)
     {
        case 1:
        {
           printf("Belongs to first category ");
         }
        break;
         
         
        case 2:
        {
            printf("Belongs to second category ");
        }
        break;
        
        case 3:
        {
             printf("Belongs to third category ");
        }
        break;
        
        case 4:
        {
            printf("Belongs to fourth category ");
        }
        break;
        
        case 5:
        {
            printf("Belongs to fifth category ");
        }
        break;
        
        case 6:
        { 
             printf("Belongs to sixth category ");
        }
        break;
     }
     
     BMI = (weight / (height * height));
     printf(" BMI is:, %.1f \n", BMI);
     
     if ( BMI <= 18.5)
     {
        printf("Underweight\n");
     }
     else if (BMI > 18.5 && BMI<= 25)
     {
        printf("Normal\n");
     }
     else if (BMI > 25 && BMI <=30)
     {
        printf("Overweight\n");
     }
     else if (BMI > 30)
     {
        printf("Obese\n");
     }
     
     
    return 0;
} 

Aucun commentaire:

Enregistrer un commentaire