2

Its not complete, but in doesnt print anything

char *fruit[] = {"rasberry", "cherry"};
char *veg[] = {"salad", "tomato"};
char word;


printf("Print a veg or a fruit \n");
scanf("%s", &word);


for (int i = 0; i < strcmp(fruit, veg ); i++)
{
    if (word == fruit[i])
        printf("Its fruit!");
}

how to compare it?

  • 1
    What are you expecting `strcmp(fruit, veg)` to do? Did you mean to have `i < 2` (2 because there are 2 elements in `fruit`) instead? – wolfPack88 Oct 31 '14 at 20:09
  • 1
    i want that to find a word that user wrote. i ve tried to do i < 2. it didnt work – Viktorija Kočerha Oct 31 '14 at 20:13
  • 1
    @ViktorijaKočerha: what exactly do you want? check below I posted to check if user entered fruit. You can apply same logic to check if user entered veg. – Giorgi Moniava Oct 31 '14 at 20:17
  • 1
    no, i want it to print what is it. a fruit or a veg – Viktorija Kočerha Oct 31 '14 at 20:21
  • this line: char *fruit[] = {"rasberry", "cherry"}; defines a two element char *fruit[], and two literal strings rasberry and cherry. SO to reference cherry, would be *fruit[1] this same pattern of code needs to be followed for both the fruit and veg references – user3629249 Nov 01 '14 at 03:08
  • this line: char word; word is a single character, yet the code is asking the user to input a whole word (probably several characters) into that variable. any character after the first is corrupting the stack, which leads to undefined behaviour – user3629249 Nov 01 '14 at 03:15
  • this parameter in the 'for' statement: i < strcmp(fruit, veg ) will always be i < 1 or i < -1. this is meaningless in the current context – user3629249 Nov 01 '14 at 03:17
  • this line: if (word == fruit[i]) compares two pointers, which will ALWAYS be different. it does not compare the contents of word with the contents of fruit[i]. – user3629249 Nov 01 '14 at 03:19
  • this line: strcmp(fruit, veg ) compares the first string in fruit with the first string in veg. a meaningless comparison, especially since those two arrays are 'preset' to different contents. – user3629249 Nov 01 '14 at 03:21

1 Answers1

4

Ok after a bit more clarification what is needed, try something like this:

    char *fruit[] = {"rasberry", "cherry"};
    char *veg[] = {"salad", "tomato"};
    char word[100]={0};


    printf("Print a veg or a fruit \n");
    scanf("%s", word);

    // Check fruit
    for (int i = 0; i < 2; i++)
    {
        if (strcmp(word,fruit[i])==0)
        {
            printf("Its fruit \n");
        }
    }
    // Check veg
    for (int i = 0; i < 2; i++)
    {
        if (strcmp(word,veg[i])==0)
        {
            printf("Its veg \n");
        }
    }
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • why did you do this char word[100] – Viktorija Kočerha Oct 31 '14 at 20:42
  • @ViktorijaKočerha: because char is for storing only one character. Did you try this, it works at my side. Some more info here: http://www.cprogramming.com/tutorial/c/lesson9.html – Giorgi Moniava Oct 31 '14 at 20:46
  • @ViktorijaKočerha: scanf needs address of variable where it will store input - that is why you used &. But in case of arrays, the name of the array can act like the pointer, it is address of first element. See here: http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c. You can read up a bit more on c basics – Giorgi Moniava Oct 31 '14 at 21:01