Gaurav
Gaurav

Reputation: 780

How to return string from a char function

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);
 }

Upvotes: 1

Views: 829

Answers (2)

retracile
retracile

Reputation: 12349

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;

Upvotes: 1

Peter Schuetze
Peter Schuetze

Reputation: 16305

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.

Upvotes: 0

Related Questions