1

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); } 
3
  • Appears to be a followup question related to stackoverflow.com/questions/1847690 and this has got to be homework... Commented Dec 4, 2009 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. Commented Dec 4, 2009 at 18:02
  • This is the same user asking the exact duplicate question again: stackoverflow.com/questions/1847690/…. Since this is newer, consider closing. Commented Dec 5, 2009 at 14:11

2 Answers 2

1

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; 
Sign up to request clarification or add additional context in comments.

3 Comments

but it is very difficult to get what u did
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.
+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/…
0

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.

2 Comments

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.
OK, changing the return type to something useful makes the whole task easier. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.