views:

195

answers:

2

I want the function getCategory() to return "invalid" , instead of printing the word "invalid" (i.e instead of using printf ) when input to the function is invalid (i.e.when either height or weight are lower then zero). please help:

#include<stdio.h>
#include<conio.h>
char getCategory(float height,float weight)
{
char invalid = '\0'; 
float bmirange;
if(height<=0 || weight<=0)
return invalid;
else
 {
height=height*0.01;       
bmirange=[weight/(height*height)];
if(bmirange< 15 )
   return starvation;
 }  
}

/* return the following to category
If bmi range < 15 then category is "starvation"
    If bmi range >=15 && bmi range < 18.5 then category is "underweight"
    If bmi range >=18.5 && bmi range < 25 then category is "normal"
    If bmi range >= 25 && bmi range < 30 then category is "overweight"
    If bmi range >=30 && bmi range < 40 then category is "obese"
    If bmi range >=40 then category is "morbidly obese
*/


int main()
 {
 char Category;
 float height,weight;
 printf("enter height");
 scanf("%f",&height);
 printf("enter weight");
 scanf("%f",&weight);
 Category=getCategory(height,weight);
 if(Category == 0)
 printf("invalid");
 else
 printf("%c", Category);
 }
+1  A: 

Change the return type to a char *, then return a pointer to your various category strings. You also need to fill in the checks for the various bmi levels.

Looks a lot like homework; please mark it as such if so.

Here's my solution... but I've randomized the order of the lines since you do need to do your own homework. There should be a few hints in this though, such as struct and the return lines.

    scanf("%f",&height);
struct bmicategory {
}
        {18.5, "normal"},
    return categories[i-1].name;
    float bmi = weight / (height * height);
    struct bmicategory categories[] = {
    printf("enter weight (in kg): ");

    };
    char *name;

    return 0;

        {40, "morbidly obese"},
        {15, "underweight"},
        }
    scanf("%f",&weight);
    for(i=1; i<sizeof(categories)/sizeof(struct bmicategory); i++) {
#include<stdio.h>
        if(bmi < categories[i].value) {
            break;
    printf("%s\n", category);
    height = height * 0.01;
    printf("enter height (in cm): ");
        {25, "overweight"},

    category=getBmiCategory(height,weight);

int main() {
    float value;
    int i;
    /* printf("BMI = %f\n", bmi); */
}
};
    float height;
        {30, "obese"},
    }
    char *category;

    char *name = NULL;
        {0, "starvation"},
char *getBmiCategory(float height, float weight) {
    float weight;
retracile
thansk for the hints
Gaurav
but it is very difficult to get what u did
Gaurav
Yeah, it is, but I wanted to make it easier to solve yourself than reverse-engineer my answer. High-level overview: I calculated the BMI, looped through an array of structures containing the BMI value and the category name and returned a pointer to the name. But you need to start with your return type.
retracile
I got the program working ,thanks again.
Gaurav
+1 because the asker forgot, and because it is a prettier answer then mine in the original (exact same!) question Gaurav asked earlier: http://stackoverflow.com/questions/1847690/how-can-i-change-my-current-function-such-that-it-returns-the-string-invalid/1847747
Abel
A: 

I don't see your problem. You can not change the return type depending on the parameters. Either always return type char or always return a string. This is a language limitation (and I would say a good one).

I can think of two ways around it. The first is return an object or pointer to Memory. However, after you get the result you still have fork your code depending on what the return type is.

In your case, you actually describe the typical use case for an exception. Throw an exception if either one of the arguments are below zero. You can than catch your exception in main with a try-catch construct and go on from there.

Peter Schuetze
From the embedded comment, it's clear that one of the changes that he needs to make for the assignment is to change the return type.
retracile
OK, changing the return type to something useful makes the whole task easier. :)
Peter Schuetze