1

On Sunday, Monday, and Tuesday; my program is supposed to ask, "Enter the number of [name of food] you can eat". It asks this question after you enter the food you want to eat. But that question is asked every day. How do I fix this?

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: ");
    scanf("%s", &food);

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

    //determines what food the picky eater would eat
    if((strchr(day, "Sunday") == 0 || strchr(day, "Monday") == 0 || strchr(day, "Tuesday")) && (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((strchr(day, "Wednesday") == 0 || strchr(day, "Thursday") || strchr(day, "Friday")) && 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") && 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;
}
zackBYE344
  • 63
  • 5

3 Answers3

1

this is invlaid code , read 'man strchr' you will see it looks for a character in a string. The compiler is surely complaining big time at you

strchr(day, "Sunday") == 0

you need

strcmp(day, "Sunday") == 0
pm100
  • 48,078
  • 23
  • 82
  • 145
0

strchr() is used to search for a character in a string. You should use strcmp() to compare two strings.

if (!strcmp(day, "Saturday") && ...) {
    // do stuff ...
}

There is also stricmp() if you want case-insensitive comparison.

if (!stricmp(day, "Saturday") && ...) { // SATURDAY is also matched
    // do stuff ...
}

One thing to keep in mind, using scanf() to read user input is dangerous. You should consider using fgets() along with sscanf() for that purpose.

char food[20];
int numFood;

printf("Enter food: ");
if (!fgets(food, sizeof food, stdin)) {
    // Handle failure... 
}

size_t newlinePos = strcspn(food, "\n"); // fgets() reads the newline delimiter
food[newlinePos] = '\0';                 // so make sure to replace it with a null-terminator

...

char tmp[20];
printf("Enter the number of %s you can eat: ", food);
if (!fgets(tmp, sizeof tmp, stdin)) {
    // Handle failure... 
}

newlinePos = strcspn(tmp, "\n");
tmp[newlinePos] = '\0';

if (sscanf(tmp, "%d", &numFood) != 1) {
    // Wrong user input
}

// The rest of your code...
Zakk
  • 1,935
  • 1
  • 6
  • 17
0

In addition to errors like using strchr instead of strcmp and char *day[20] instead of char day[20], here's the culprit:

strcmp(day, "Wednesday") == 0 || strcmp(day, "Thursday") || strcmp(day, "Friday")
//                                                          ^^^ oops?

The final comparison should be

strcmp(day, "Friday") == 0

If you don't include == 0 then any non-zero return value of strcmp evaluates to true.

guard3
  • 823
  • 4
  • 13