Home > Software engineering >  Why aren't my conditional statements working?
Why aren't my conditional statements working?

Time:10-27

I am working on conditional statements where my float variables are supposed to be in between a range.

If they are above or below the given range then they need to print out whether it's too much or too little. and if everything within range then it's supposed to print out great! etc

My code works when everything is out of bounds and if everything is in bounds, but if only 1 or 2 things are not within bounds, it still prints out that everything is great alongside that it's too much or too little.

What am I missing?

`

#include <stdio.h>

int main()
{

float salt, pepper, garlic, thyme;


printf("Lets cook!\n");
printf("====================\n");
printf("\n");
printf("Amount of salt added? ");
scanf("%f", &salt);
printf("Amount of pepper added? ");
scanf("%f", &pepper);
printf("Amount of garlic added? ");
scanf("%f", &garlic);
printf("Amount of thyme added? ");
scanf("%f", &thyme);



        if (pepper < 1.5)
                {
                printf("Needs more pepper\n");
                }
        else if(pepper > 3.5)
                {
                printf("Too much pepper!\n");
                }
                
if(salt <5.5)
{
    printf("Needs more salt\n");
}
else if(salt > 13)
{
    printf("Too salty!\n");
}

        if (garlic < 2)
                {
                printf("Needs more garlic\n");
                }
        else if(garlic > 4)
                {
                printf("Too much garlic!\n");
                }

if (thyme < 0.5)
{
     printf("Needs more thyme\n");
}
else if(thyme > 1.25)
    {
         printf("Too much thyme!\n");
    }
    
else
{
    printf("Delicious!\n");
}

        return 0;
}

`

When I only have 2 of the conditional statements triggered it still triggers the else:

Lets cook!

Amount of salt added? 15 Amount of pepper added? 5 Amount of garlic added? 3 Amount of thyme added? 1 Too much pepper! Too salty! Delicious

But if all conditional statements are triggered the else doesn't populate:

Lets cook!

Amount of salt added? 15 Amount of pepper added? 15 Amount of garlic added? 15 Amount of thyme added? 15 Too much pepper! Too salty! Too much garlic! Too much thyme!

And if everything is between the range, the else triggers (like how I want it to):

Lets cook!

Amount of salt added? 6 Amount of pepper added? 2 Amount of garlic added? 3 Amount of thyme added? 1 Delicious!

How do I get the else to not trigger when only some of them are not within range?

CodePudding user response:

The else in the posted code is related to the last if ... else if ... else construct only, not to the other if ... else if ..., which are separate statements.

Consider adding a variable counting the in bound cases and changing all those ifs.

int within_range = 0;

if (pepper < 1.5) {
    printf("Needs more pepper\n");
} else if (pepper > 3.5) {
    printf("Too much pepper!\n");
} else {
      within_range;
}
             
if (salt < 5.5) {
    printf("Needs more salt\n");
} else if (salt > 13) {
    printf("Too salty!\n");
} else {
      within_range;
}

if (garlic < 2) {
    printf("Needs more garlic\n");
} else if (garlic > 4) {
    printf("Too much garlic!\n");
} else {
      within_range;
}

if (thyme < 0.5) {
     printf("Needs more thyme\n");
} else if (thyme > 1.25) {
     printf("Too much thyme!\n");
} else {
      within_range;
}    

if ( within_range == 4 ) {
    printf("Delicious!\n");
}

CodePudding user response:

The reason is that the "Delicious" comment is only output when both the thyme ingredient checks succeed.

One way of implementing this sort of thing is to introduce variables to hold the state of the comparisons. It's also a technique to write self-documenting code that is quickly understandable by a (perhaps ignorant) reader.

So instead of checking the variable every time, assign the result to a boolean / integer:

pepper_low  = ( pepper < 1.5 );
pepper_high = ( pepper > 3.5 );
pepper_ok   = !( pepper_low || pepper_high );

Then later on, the code can check pepper_ok, e.g.: if ( pepper_ok ). Which is clear and concise, but at the cost of introducing variables.

This makes your final check for deliciousness something like:

if ( salt_ok && pepper_ok && garlic_ok && thyme_ok )
{
    printf("Delicious!\n");
}

For a large number of ingredients, discrete variables like this would become unwieldy. In this case you could use an array of Ingredient structures.

#include <stdio.h>

int main()
{
    float salt, pepper, garlic, thyme;
    int   salt_low,  pepper_low,  garlic_low,  thyme_low;
    int   salt_high, pepper_high, garlic_high, thyme_high;
    int   salt_ok,   pepper_ok,   garlic_ok,   thyme_ok;

    printf("Lets cook!\n");
    printf("====================\n");
    printf("\n");
    printf("Amount of salt added? ");
    scanf("%f", &salt);
    printf("Amount of pepper added? ");
    scanf("%f", &pepper);
    printf("Amount of garlic added? ");
    scanf("%f", &garlic);
    printf("Amount of thyme added? ");
    scanf("%f", &thyme);

    salt_low  = ( salt < 5.5 );
    salt_high = ( salt > 13.0 );
    salt_ok   = !( salt_low || salt_high );
    if( salt_low )
    {
        printf("Needs more salt\n");
    }
    else if ( salt_high )
    {
        printf("Too salty!\n");
    }

    pepper_low  = ( pepper < 1.5 );
    pepper_high = ( pepper > 3.5 );
    pepper_ok   = !( pepper_low || pepper_high );
    if ( pepper_low )
    {
        printf("Needs more pepper\n");
    }
    else if ( pepper_high )
    {
        printf("Too much pepper!\n");
    }

               
    // TODO: Repeat for other ingredients

    if ( salt_ok && pepper_ok && garlic_ok && thyme_ok )
    {
        printf("Delicious!\n");
    }

    return 0;
}
  • Related