Why this boolean doesn't give me the right value?

61 Views Asked by At

First of all, my program functions, but the b3 expression (a boolean) does not function like I want to. Instead of giving me a value of 0 (false) when I select that one train is DIESEL or ELECTRICAL, it gives me a value of 1 (TRUE) when some of the trains are DIESEL or ELECTRICAL. It only gives false when both trains are ELECTRICAL or both DIESEL. When one train is ELECTRICAL and the other DIESEL, it keeps me giving me 1 (TRUE). My code is more extensive but these are the parts related to the problem that I account for.

typedef enum{ELECTRICAL, CARBON, SOLAR, GASOLINE, DIESEL, MAGNETIC} tPropulsion;    
typedef enum{FALSE, TRUE} boolean;    
int main()
{
    tPropulsion propulsionType1, propulsionType2;
    boolean b2, b3, b4;

    printf("Insert type of propulsion of train1>> 0-ELECTRICAL, 1-CARBON, 2-SOLAR, 3 GASOLINE, 4-DIESEL, 5-MAGNETIC ");
    scanf("%u", &propulsionType1);

    printf("Insert type of propulsion of train2>> 0-ELECTRICAL, 1-CARBON, 2-SOLAR, 3 GASOLINE, 4-DIESEL, 5-MAGNETIC ");
    scanf("%u", &propulsionType2);

    b3 = propulsionType1 != DIESEL && propulsionType2 != ELECTRICAL && 
    propulsionType1 != DIESEL && propulsionType2 != ELECTRICAL;


    printf("Neither train 1 or train 2 are DIESEL or ELECTRICAL is (0-FLASE, 1-TRUE)>> %u", b3);
}
1

There are 1 best solutions below

3
Guillaume D On

First of all:

  • indent your code
  • use parenthesis
  • rather than using enum for boolean, you could do your own boolean like this:
#define TRUE (0==0)
#define FALSE !TRUE

Then clarify your statement, what do you want?

"if train 1 or train 2 is DIESEL or ELECTRICAL then TRUE, else FALSE"

    b3 = ((propulsionType1 == DIESEL) || (propulsionType2 == ELECTRICAL)) ||
    ((propulsionType1 == DIESEL) || (propulsionType2 == ELECTRICAL));

Then look at propulsionType1 and propulsionType2

    b3 = ((propulsionType1 == DIESEL) || (propulsionType1 == ELECTRICAL)) ||
    ((propulsionType2 == DIESEL) || (propulsionType2 == ELECTRICAL));