-2

i wanted to compare user input with text in c, but i dont know exactly how. Please help me.

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

char len_input[16];
int input;

void name() {
        printf("Enter your name\n");

    input = fgets(len_input, sizeof(len_input), stdin);
    input;
    if (input == "Osas") {
             printf("What a stupid name...\n");
     }
     else {
             printf("Cool name dude!\n");
     }
}

int main(int argc, char **argv) {
    name();
}

I tried this but its not working. Please show it how to do it correctly becaus im new to c. Thanks

microman
  • 73
  • 8
  • 2
    You are probably looking for the `strcmp()` function. – Anoop R Desai Aug 11 '19 at 18:44
  • 1
    https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings – WhozCraig Aug 11 '19 at 18:45
  • You need a book on the C language, like "The C programming language" by Kernighan and Ritchie. String comparison is one of the first topics discussed there – ForceBru Aug 11 '19 at 18:45
  • 2
    Note that `input = fgets(...)` is assigning a value to the wrong type (`int` instead of `char *`) which the compiler should have warned about. Please enable and act on all warnings. Worse, you can't compare a string with `==` in C. There is also a problem with `fgets` retaining the **newline** typed. Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Aug 11 '19 at 18:49
  • In [How do I properly compare strings?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings) pay attention to the link provided in @chux answer if you would like to write your own `strcmp` function. – David C. Rankin Aug 11 '19 at 19:02

1 Answers1

-1

Try:

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

char len_input[16];

void name() {
    printf("Enter your name\n");

    fgets(len_input, sizeof(len_input), stdin);
    len_input[strcspn(len_input, "\n")] = 0;

    if (strcmp(len_input, "Osas") == 0) {
             printf("What a stupid name...\n");
     }
     else {
             printf("Cool name dude!\n");
     }
}

int main(int argc, char **argv) {
    name();
}

Update 1: Incorporated @Weather Vane's comment about removing new-lines before comparing.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Anoop R Desai
  • 712
  • 5
  • 18
  • Although `fgets` retains any newline, it is false to assume there will *always* be a newline (for example the last line of input redirected from a file, or a truncated input). Better to remove any newline from the input. – Weather Vane Aug 11 '19 at 18:52
  • Agreed! Also just had a look at your comment on the question. I must admit I was not aware of the `strcspn()` function. Thanks for pointing it out. – Anoop R Desai Aug 11 '19 at 18:58
  • @WeatherVane - Updated code as per your suggestion. – Anoop R Desai Aug 11 '19 at 19:09
  • I made a little update that should make this a bit more clear. – S.S. Anne Aug 11 '19 at 19:46
  • @JL2210 I'd be interested to know why you changed the newline removal method – it will segfault if there isn't one – and there might not be. The one I linked above is neat and safe. Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Aug 11 '19 at 20:13
  • @WeatherVane Good point. Reverted. – S.S. Anne Aug 11 '19 at 20:14