This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

my function keeps returning the wrong value

TbooleanVALUE showNUMBER ( int number )
{

switch (number)
 {
 case 0: printf("0"); break;
 case 1: printf("1"); break;
 case 2: printf("2"); break;
 case 3: printf("3"); break;
 case 4: printf("4"); break;
 case 5: printf("5"); break;
 case 6: printf("7"); break;
 case 7: printf("8"); break;
 case 8: printf("9"); break;
 case 9: printf("9"); break;
 }

if ( number > 5 )
  return FALSE ;

if ( number < 2 )
  return TRUE;

  return 2;

}



Parents Reply Children
  • its done like this

    #define TbooleanVALUE float

  • Go to the penalty box for badly selected data type name.

    A float is most definitely not a data type that should be hidden behind a data type that can only have the logical values true or false.

    By the way - how do you verify if your function returns the value 2 or not? Remember that there are problems with comparing floating point values, checking for exact matches.

  • i think a float can store the number 0 and 1 and 2?

    i know it returns 2 cause it is not 0 or 1.

  • What value are you passing to the function?

    If you give it a value of 2, 3, 4 or 5 then the function should return 2.

    What are TRUE and FALSE defined as? A boolean is normally used to represent YES or NO, TRUE or FALSE. It is not (normally) used to return YES or NO or 'something else'.

  • Yes, a float can store lots of numbers.

    But why you you give it a name Boolean, when your intention isn't to use it as a boolean? 2 isn't a boolean value.

    Some boolean logic.

    a := true;
    b := false;
    
    a != b;
    a == !b
    b == !a
    a == !!a
    b == !!b
    

    But what happens with your value 2?
    What is !2? Is it true? Is it false?
    What is !!2? Is it back to 2? Or undefined? Or what?
    Will !!2 be same as 2?

    When you have a data type with the name boolean, that is a promise to the reader that the data type is intended for a boolean state - not suddenly some random numeric value.

    Another thing - why #define when the language have a specific keyword to define a data type?