0

Like the title says, I can only force exit by using Ctrl-C/Z.

I get stuck in a never ending fgets() do-while loop

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

int main() {

    int loop = 1;
    char line[255];

    printf("Running, enter a line: \n");

    do {
        fgets(line, sizeof(line), stdin);

        if (strcmp("exit", line) == 0){
            loop = 0;
        }

        printf("%s", line);

    } while(loop == 1);

    printf("\nNever displays...\n");

    return 0;
}
Yusef Maali
  • 2,201
  • 2
  • 23
  • 29
HamzaKerem
  • 121
  • 1
  • 8
  • You should know definition about ```fgets()``` function. This problem will be solved easily. – Land Jun 07 '20 at 02:28
  • I recommend [the solution in this answer](https://stackoverflow.com/a/28462221/3386109). – user3386109 Jun 07 '20 at 02:30
  • Use ```strstr()``` at the place of ```strcmp```. It'll solve your problem. – Shubham Jun 07 '20 at 02:38
  • 2
    @Lucas, `strstr` will also ignore any other incorrect content of `line` nd not only ignore the line endig. It will also return non-NULL result when comparing with `"Never exit!!!\n"` which clearly not what is the intention of the used `strcmp`. – Gerhardh Jun 07 '20 at 06:03

0 Answers0