By user2651382


2013-08-11 04:23:53 8 Comments

I'm trying to get a string input from the user and then run different functions depending on the input they've entered.

For example, say I asked, "What is your favorite fruit?" and I want the program to comment depending on what they enter...I'm not sure how to do this. Here's what I have so far:

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

char fruit[100];

main() {
    printf("What is your favorite fruit?\n");
    fgets (fruit, 100, stdin);
    if (strcmp(fruit, "apple")) {
        printf("Watch out for worms!\n");
    }
    else {
        printf("You should have an apple instead.\n");
        }

}

When I run the program, no matter what I enter, it never does the else statement.

Thanks for your help!

3 comments

@lulyon 2013-08-11 05:22:38

Note two things in your code:

  1. fgets keeps the trailing '\n'. the associated char in fruit should be replaced with a '\0' before comparing with the string "apple".
  2. strcmp return 0 when two strings are the same, so the if clause should be changed based on what you mean.(The fruit and "apple" be equivalent in the if clause)
  3. Standard usage of C main function is int main(){ return 0;}

The revised code:

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

char fruit[100];

int main() {
    printf("What is your favorite fruit?\n");
    fgets (fruit, 100, stdin);
    fruit[strlen(fruit)-1] = '\0';
    if (strcmp(fruit, "apple") == 0) {
        printf("Watch out for worms!\n");
    }
    else {
        printf("You should have an apple instead.\n");
    }
    return 0;
}

@Madan Ram 2013-08-11 05:26:57

The above is the correct one, Because '\n' is taken by fgets as the end of string so replace it with '\0' is correct.

@luser droog 2013-08-11 04:25:59

strcmp returns 0 if the inputs match, some value>0 if the left is "greater" than the right, some value<0 if the left is "lesser" than the right. So usually you want to simply test for equality with strcmp(...)==0. But there is also the clever version: !strcmp(...). Even if you don't use this style, it's useful to learn to recognize it.

And remember that fgets does not remove the newline character '\n' from the string.

@0decimal0 2013-08-11 04:42:03

It removes if the length of string is arraysize-1.

@luser droog 2013-08-11 05:16:15

Even then I wouldn't say that the newline is removed, but maybe withheld or pending.

@0decimal0 2013-08-11 05:25:58

I should have used quotes around removes , It ain't the right word obviously ..... actually it retains '\n' if string is small enough for it to be accomodated otherwise it doesn't .

@Jonathan Leffler 2013-08-11 07:24:10

Note that your description of the return values of strcmp() is wrong; the values will be 0 on equality (you said that too), but when the first argument is 'smaller' than the second, the return value will just be negative, not necessarily -1; similarly, for 'greater', the return value will be positive, not necessarily +1. See strcmp() behaviour in 32-bit and 64-bit systems

@Sarat 2013-08-11 04:31:21

Change the if condition to the following:

if(strcmp(fruit,"apple") == 0)

strcmp returns 0 strings if match. You should always compare the result using == operator

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite

1 Answered Questions

[SOLVED] using fgets() with stdin as input: ^D fails to signal EOF

  • 2018-10-13 15:59:53
  • ctt25
  • 300 View
  • 1 Score
  • 1 Answer
  • Tags:   c fgets eof

1 Answered Questions

[SOLVED] Why does this user input to fgets() cause the program to abort?

  • 2018-04-08 19:41:40
  • yalpsid eman
  • 160 View
  • 2 Score
  • 1 Answer
  • Tags:   c crash fgets

1 Answered Questions

fgets has more characters that its supposed to

1 Answered Questions

[SOLVED] What will cause fgets() to continuously wait for input?

2 Answered Questions

3 Answered Questions

[SOLVED] scanf and strcmp with c string

Sponsored Content