Thread: Conditional Statement Issues

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    3

    Question Conditional Statement Issues

    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?

    Code:
    #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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2022
    Posts
    3
    I've realized that the last else statement is only referencing the previous if, else if statement-- not all of them like I originally thought.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Consistent code formatting would definitely have helped to find this issue earlier.
    Code:
    #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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Yes, the "else" only attaches to the last of the "if"/"else if" constructs.

    This is one possible fix but it will only print out one of the out-of-bounds ingredients.
    Code:
    if (a < 1)
        printf("...");
    else if (a > 9)
        printf("...");
    else if (b < 1)
        printf("...");
    else if (b > 9)
        printf("...");
    else
        printf("yum!\n");
    To print out all of the offending ingredients you could use a flag.
    Code:
    int okay = 1;
     
    if (a < 1) {
        printf("...");
        okay = 0;
    }
    else if (a > 9) {
        printf("...");
        okay = 0;
    }
     
    if (b < 1) {
        printf("...");
        okay = 0;
    }
    else if (b > 9) {
        printf("...");
        okay = 0;
    }
     
    if (okay)
        printf("yum!\n");
    Or if you want to get fancy:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
     
    typedef struct Ingredient {
        const char *name;
        float lower, upper;
        float amount;
    } Ingredient;
     
    int main()
    {
        Ingredient ingred[] = {
            { "pepper", 1.5,  3.50, 0 },
            { "salt",   5.5, 13.00, 0 },
            { "garlic", 2.0,  4.00, 0 },
            { "thyme",  0.5,  1.25, 0 }
        };
        int len = sizeof ingred / sizeof ingred[0];
     
        printf("Lets cook!\n");
        printf("====================\n");
     
        for (int i = 0; i < len; ++i) {
            printf("Amount of %s added? ", ingred[i].name);
            scanf("%f", &ingred[i].amount);
        }
     
        bool okay = true;
        for (int i = 0; i < len; ++i) {
            if (ingred[i].amount < ingred[i].lower) {
                printf("Needs more %s\n", ingred[i].name);
                okay = false;
            }
            else if (ingred[i].amount > ingred[i].upper) {
                printf("Too much %s\n", ingred[i].name);
                okay = false;
            }
        }
     
        if (okay)
            printf("Delicious!\n");
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-15-2014, 01:50 PM
  2. parallelogram for statement issues
    By surfingbum18 in forum C Programming
    Replies: 30
    Last Post: 03-08-2009, 10:14 PM
  3. regarding conditional statement in xml
    By cnu_sree in forum C Programming
    Replies: 5
    Last Post: 07-21-2007, 10:22 PM
  4. input/switch statement issues
    By peanut in forum C Programming
    Replies: 5
    Last Post: 10-27-2006, 02:58 PM
  5. Issues with if statement
    By tameeyore in forum C Programming
    Replies: 7
    Last Post: 09-27-2004, 09:34 PM

Tags for this Thread