0

Say that I have an array that holds the names of various items for purchase at a fast food place...

char options[8][15] = {"burger", "fries", "pop", "apples", "vegan burger", "vegan fries"};

and an uninitialized variable that will hold the customer's order...

char choice[15];

I am using a function exists_in_array() to iterate through the options array and check if the choice variable matches any of the strings contained inside the array.

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

...

int exists_in_array(char value[], char (array) [8][15])
{
    for(int i = 0; i < 8; i++)
    {
        if(strcmp(value, array[i]) == true)
        {
            return true;
        }
    }
    return false;
}

...

int main(void)
{
    char options[8][15] = {"burger", "fries", "pop", "apples", "vegan burger", "vegan fries"};
    char choice[15];

    printf("Enter your choice: ");
    fgets(choice, 15, stdin);
    strtok(choice, "\n");    // Removing the trailing newline that gets recorded with fgets

    if (exists_in_array(choice, options))
    {
        printf("That is a valid option");
    }
    else
    {
        printf("That is not a valid option");
    }
}

I am not getting any error messages at the moment, but the only option that will ever return That is a valid option is burger. Every other item will say That is not a valid option and I can't figure out why this is happening. It can't be the newline character that gets added with fgets() because I use strtok() to remove it immediately afterwards.

Can anyone help me figure this out?

Shock9616
  • 116
  • 9
  • 1
    Not sure what documentation you're looking at but `strcmp` doesn't return true/false. – jarmod Oct 26 '20 at 21:37
  • It returns a 1 or 0 right? true or false are placeholders for 1 and 0 from the `stdbool.h` header file that I included to help readability. – Shock9616 Oct 26 '20 at 21:38
  • No, it returns negative, zero, or positive values. While true/false might also have values 1/0, I don't think you should use them. The interface is not "returns true/false as defined in stdbool.h". – jarmod Oct 26 '20 at 21:40
  • You really should read the documentation instead of guessing. Especially when there is a conflict between your guess and a feedback here... – Yunnosch Oct 26 '20 at 21:41
  • ah ok I will do that – Shock9616 Oct 26 '20 at 21:41
  • 1
    Change `strcmp(value, array[i]) == true` to `strcmp(value, array[i]) == 0`. – goodvibration Oct 26 '20 at 21:41

2 Answers2

1

From the documentation for strcmp():

strcmp() returns an integer indicating the result of the comparison, as follows:

  • 0, if the strings s1 and s2 are equal;
  • a negative value if s1 is less than s2;
  • a positive value if s1 is greater than s2.

Your check should be:

if (!strcmp(value, array[i])) { // ...
// Or, more explicitly:
if (strcmp(value, array[i]) == 0) { // ...
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Ok thanks I will try that. This seems like a really counter-intuitive way to design the function in my opinion, but I guess there are reasons for having it this way. – Shock9616 Oct 26 '20 at 22:25
  • Well counter intuitive or not, that depends, see https://stackoverflow.com/questions/595450/why-does-strcmp-return-0-when-its-inputs-are-equal – Marco Bonelli Oct 26 '20 at 22:30
  • Ah ok I see. That makes more sense thank you! Just coming from the perspective of a newcomer to the language, that's not the way that one might expect it to work without having it explained to them. – Shock9616 Oct 26 '20 at 22:33
0
int exists_in_array(const char *value, char **options)
{
    while(options && *options)
    {
        if(!strcmp(value, *options))
        {
            return true;
        }
        options++;
    }
    return false;
}

int main(void)
{
    char *options[] = {"burger", "fries", "pop", "apples", "vegan burger", "vegan fries", NULL};
    char choice[15];

    printf("Enter your choice: ");
    fgets(choice, 15, stdin);
    if(*choice) choice[strlen(choice)] = 0; 

    if (exists_in_array(choice, options))
    {
        printf("That is a valid option");
    }
    else
    {
        printf("That is not a valid option");
    }
}
0___________
  • 60,014
  • 4
  • 34
  • 74