-1

I am coding something and this should print "Good Job" but instead it is just saying "invalid lol,".

Here is my code:

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

fgets(line, 69, stdin);
if (strcmp(line, "stats") == 0)
   {
       printf("Good Job\n");
   }
else
   {
       printf("Invalidlol\n");
   {
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Nerdboi
  • 1
  • 2
  • 1
    See [mcve]..... – user3386109 Sep 25 '20 at 23:02
  • 1
    Echoing what @user3386109 said, this is not a valid program as it does not have a `main` function. Also, what did you type in as the input? – Daniel Walker Sep 25 '20 at 23:03
  • 5
    Note that the string returned by `fgets` *includes* the trailing newline character. See here: https://stackoverflow.com/q/2693776/10871073 – Adrian Mole Sep 25 '20 at 23:04
  • Does this answer your question? [String compare in c with fgets](https://stackoverflow.com/questions/41252808/string-compare-in-c-with-fgets) – smac89 Sep 25 '20 at 23:07

1 Answers1

1

I suspect the newline is getting added to your string. Try

if (strcmp(line, "stats\n") == 0)

For a quick fix

John b
  • 1,338
  • 8
  • 16