Take the 2-minute tour ×

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:

char getCategory(float height,float weight)
char invalid = '\0'; 
float bmirange;
if(height<=0 || weight<=0)
return invalid;
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");
 printf("enter weight");
 if(Category == 0)
 printf("%c", Category);
share|improve this question
Appears to be a followup question related to stackoverflow.com/questions/1847690 and this has got to be homework... –  retracile Dec 4 '09 at 17:25
height=height*0.01; This, btw, is what Apps Hungarian notation is for, as advocated by Joel Spolsky. mheight = cmHeight * 0.01; makes sense without the comment. –  Steve Jessop Dec 4 '09 at 18:02
This is the same user asking the exact duplicate question again: stackoverflow.com/questions/1847690/…. Since this is newer, consider closing. –  Abel Dec 5 '09 at 14:11

2 Answers 2

up vote 1 down vote accepted

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.

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"},
    for(i=1; i<sizeof(categories)/sizeof(struct bmicategory); i++) {
        if(bmi < categories[i].value) {
    printf("%s\n", category);
    height = height * 0.01;
    printf("enter height (in cm): ");
        {25, "overweight"},


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;
share|improve this answer
thansk for the hints –  Gaurav Dec 4 '09 at 18:00
but it is very difficult to get what u did –  Gaurav Dec 4 '09 at 18:03
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 Dec 4 '09 at 18:21
I got the program working ,thanks again. –  Gaurav Dec 4 '09 at 21:03
+1 because the asker forgot, and because it is a prettier answer then mine in the original (exact same!) question Gaurav asked earlier: stackoverflow.com/questions/1847690/… –  Abel Dec 5 '09 at 14:13

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.

share|improve this answer
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 Dec 4 '09 at 17:49
OK, changing the return type to something useful makes the whole task easier. :) –  Peter Schuetze Dec 4 '09 at 20:00

Your Answer


