views:

239

answers:

6

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;        //1 centimeter = 0.01 meters
        bmirange=[weight/(height*height)];

        if(bmirange< 15 )
            return starvation;
    } 
}

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: 

The getCategory method doesn't always return (because of the if statement). Also, not sure about the height in if statement. Add another return invalid at the end of the method.

char getCategory(float height,float weight)
{
    char invalid;
    if(height<=0 || weight<=0)
       return invalid;
    return 0
}
Jean Barmash
don't correct, instruct. :)
KevinDTimm
You have to initialize invalid...
micmoo
"have to" or "should"? It might depend on the original asker's intend...
Abel
A: 

you need to (very carefully) pore over your textbook to ascertain the multitude of errors in the above code.
1, your test in getCategory will almost certainly not do what you want it to do.
2, you ARE returning invalid in some cases (but not all, see #1). However, there is no way to know that as invalid has no known value.
3. in other cases, getCategory returns no value at all

KevinDTimm
#3: so it will set the retval to a rather undefined value
Abel
it may pop a value from the stack, and it may core dump. Or something else entirely different.
KevinDTimm
yup, exactly. So, "no value" is "a/any value" :)
Abel
no, it returns no value. there might be something on the stack, but the function didn't return it.
KevinDTimm
If you write `char c = getX()` and getX does not return a value, then `c` will be set to an undefined value, not to no value (which is in this scenario impossible). This is because, and correct me if I'm wrong here, the compiler will use a particular register for the ret value, which is the read and stored in `c`. Whether this register is set doesn't matter, this is part of the calling convention and the ret value is treated specially (normally either on the beginning of the stackframe or at the end, but either way, this space is reserved and exists).
Abel
we're splitting hairs, I'm not saying that you're not right as a value will be given to 'c' in your example. But, in the explicit sense, no value is RETURNED from the function.
KevinDTimm
A: 

You're defining a variable named invalid. Its contents are undefined (it could be anything from -128 to 127). When you return this variable you're returning anything; do you want to assign something to the invalid variable before you return it? e.g.


char invalid;
invalid = 'i';
if ( ... ) {
  return invalid;
} else {
  return 0;
}
PP
You can drop the bounds, it can be anything since this is Undefined Behavior (e.g. it often is whatever is left in the register)
MSalters
+2  A: 
Abel
if we're just going to answer the question, without instruction, then just do: return (height <= 0 || weight <= 0) ? 'i' : 'v';
KevinDTimm
how "without instruction"? Thought I was rather lengthy on my instruction. Anything missing? Refresh perhaps?
Abel
I would bet he'll copy your code and it will work, he won't read the answer (notes on code) and so he'll be back to ask again. That's wny I like my answer more :)
KevinDTimm
I agree to that, but that is up to the asker. If he wants to learn, he will. If not, nothing can change that and I won't force it.
Abel
@kevindtimm: lot's more explanation now and no new code. See? I listened to you ;-)
Abel
A: 

What does invalid should be mapped to? You should have a convention like this:

char invalid_category = '?';

or perhaps:

#define INVALID_CATEGORY '?'

This is better defined outside of the getCategory function so that the calling code can access it.

Also it isn't evident what your code returns when valid arguments are passed to it.

kgiannakakis
A: 

By the way, in your function getCategory, you have a variable that is not used nor declared - starvation. Where does that come from? I doubt that is a global variable.

Also, the variable bmirange does not make sense nor would it compile

bmirange=[weight/(height*height)];

as you can see that is a left hand side expression (LHS) but you have used an array subscript operators on the right hand side of expression (RHS). That is an illegal statement!

What was your intention there? Was that meant to be a pair of parenthesis?

Can you confirm this?

A lot of the answers are confusing because the OP did not make themselves clear on what is the error nor an explanation as to what is going on which is leading others to end up with code posted that does not satisfy the OP.

Hope this helps, Best regards, Tom.

tommieb75