0

I am trying to get user input with fgets in C and am having trouble using the while loop.

Here is the code I am using:

char input[300];

fgets(input, 300, stdin);

while(strcmp("Quit", input) != 0) {

    fgets(input, 300, stdin);

}

When I enter Quit the loop continues and does not terminate and I don't understand why this is so.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
code
  • 69
  • 1
  • 9

1 Answers1

1

fgets() stores the new line character in input as well. Either remove it manually, or compare like this:

while(strcmp("Quit\n", input) != 0)
Yu Hao
  • 119,891
  • 44
  • 235
  • 294