Home > Software engineering >  My If/Else Statement is Printing at the Wrong Time (updated)
My If/Else Statement is Printing at the Wrong Time (updated)

Time:02-17

When I enter food and a day, my program is supposed to figure out what the correct food to eat. If the user puts in the correct food, my program should say, "Mmmm... can't wait to eat [name of food] today!!!". Instead, my program keeps on saying "Sorry, on [days] I can't eat that..." after I enter a type of food and day. Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char**argv)

{
    char day[20];
    char food[20];
    int numFood;

    printf("Enter food: ");
    fgets(food, 20, stdin);

    printf("Enter day: ");
    scanf("%s", day);

    //determines what food the picky eater would eat
    if((strcmp(day, "Sunday") == 0 || strcmp(day, "Monday") == 0 || strcmp(day, "Tuesday") == 0) && (food[0] != 'm' && food[0] != 'k'))
    {
       printf("Mmmm...can\'t wait to eat %s today!!!\n", food);
       printf("Enter the number of %s you can to eat: ", food);
       scanf("%d", &numFood);

       if(numFood > 3)
       {
           printf("That\'s a lot of %s!", food);
           exit(0);
       }
    }
    else
    {
        printf("Sorry, on Sundays/Mondays/Tuesdays I can\'t eat that...");
        exit(0);
    }
    if((strcmp(day, "Wednesday") == 0 || strcmp(day, "Thursday") == 0 || strcmp(day, "Friday") == 0) && food[0] != 'j')
       {
           printf("Mmmm...can\'t wait to eat %s today!!!", food);
           exit(0);
       } else {
            printf("Sorry, on Wednesday/Thursday/Friday I can\'t eat that...");
            exit(0);
        }


    if(strcmp(day, "Saturday") == 0 && strlen(day) <= 7 && food[0] == 'p')
    {
        printf("\nMmmmm...can\'t wait to eat %s today!!!", food);
        exit(0);
    } else {
        printf("\nSorry, on Saturdays I can\'t eat that...");

    }

    return 0;
}

CodePudding user response:

Ok now I see your issue

Here you have

if ((strcmp(day, "Sunday") == 0 || strcmp(day, "Monday") == 0 || strcmp(day, "Tuesday") == 0) && (food[0] != 'm' && food[0] != 'k'))
{
    printf("Mmmm...can\'t wait to eat %s today!!!\n", food);
    .......

}
else
{
    printf("Sorry, on Sundays/Mondays/Tuesdays I can\'t eat that...");
    exit(0);
}

(Omitting stuff from the middle). So either you match the first complex issue or print sorry.

I suspect you want

    if ((strcmp(day, "Sunday") == 0 || strcmp(day, "Monday") == 0 || strcmp(day, "Tuesday") == 0))
    {

      if(food[0] != 'm' && food[0] != 'k')
      {
        printf("Mmmm...can\'t wait to eat %s today!!!\n", food);
        printf("Enter the number of %s you can to eat: ", food);
        scanf("%d", &numFood);

        if (numFood > 3)
        {
            printf("That\'s a lot of %s!", food);
            exit(0);
        }
     }
     else
     {
        printf("Sorry, on Sundays/Mondays/Tuesdays I can\'t eat that...");
        exit(0);
     }
  }

CodePudding user response:

For the most part I think your code is doing what you expect it to do but there a couple of spots where you have some minor issues with your if-else conditional statements.

Aside from adding \n characters to keep stdout a bit cleaner, I left your code mostly unchanged except for adjusting the conditionals as I have mentioned already.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char**argv)

{
    char day[20];
    char food[20];
    int numFood;

    printf("Enter food: ");
    fgets(food, 20, stdin);

    printf("Enter day: ");
    scanf("%s", day);

    // I added a null-terminator for printing sakes.
    size_t food_length = strlen(food);
    food[food_length - 1] = '\0';

    //determines what food the picky eater would eat
    if((strcmp(day, "Sunday") == 0 || strcmp(day, "Monday") == 0 || strcmp(day, "Tuesday") == 0))
    {

// Here I split-up your condition into 2 separate parts that prevent
// falling into the next block as you had it originally

        if (food[0] != 'm' && food[0] != 'k') {
           printf("Mmmm...can\'t wait to eat %s today!!!\n", food);
           printf("Enter the number of %s you can to eat: ", food);
           scanf("%d", &numFood);

           if(numFood > 3)
           {
               printf("That\'s a lot of %s!\n", food);
               exit(0);
           }
        } else {
            printf("Sorry, on Sundays/Mondays/Tuesdays I can\'t eat that...\n");
            exit(0);

        }

/*
 *So now instead of having your `if-else` print out on everyday entered
 *thats *not* matched once you find your day using `strcmp` you enter
 *that block based off the condition.
 */

    } else if ((strcmp(day, "Wednesday") == 0 || strcmp(day, "Thursday") == 0 || strcmp(day, "Friday") == 0))
    {
           if (food[0] != 'j') {

           printf("Mmmm...can\'t wait to eat %s today!!!\n", food);
           exit(0);
       } else {

/*
 *Here for instance you would always print out this part, on say input
 *of `Sunday` where if there aren't `if-else` checks but using separate
 *`if` blocks like you had the `else` would always be met on days that
 *weren't matched
 */

            printf("Sorry, on Wednesday/Thursday/Friday I can\'t eat that...\n");
            exit(0);
        }

    }
    else if(strcmp(day, "Saturday") == 0)
    { 
        if (food[0] == 'p') {
            printf("Mmmmm...can\'t wait to eat %s today!!!\n", food);
            exit(0);
        } else {
            printf("Sorry, on Saturdays I can\'t eat that...\n");
        }
    }

    return 0;
}

You can see where I added the comments in your code that you needed to set up your conditionals by splitting up the check between food and day of the week. More importantly was using the else-if where before you used separate blocks of if-else that will trigger after say a day Monday is found. Without you exiting this block explicitly now you know the day is Monday so you won't trigger the Wednesday condition but there was a separate if-else so your else will end up printing everytime like you mentioned.

  • Related