Meh
Meh

Reputation: 141

Terminating program on an empty string in C

So, I am very new to C and I am trying to terminate my program on an empty string (when the user presses enter with nothing there) but for some reason it is giving me an infinite loop.

Here is my code:

while(input[0] != '\0') {
    if(slot < 27 && slot >= 0) {
        struct LinkedList curr = files[slot];
        if(strcmpci(input, curr.val) == 0) {
            printf("%s, \n", curr.val);
        }

        while(curr.next != NULL) {
            curr = (*(curr.next));
            if(strcmpci(input, curr.val) == 0) {
                printf("%s\n", curr.val);
            }
        }
    }
}

and everywhere that I have went has told me to end my loop with this format

while(____ != '\0') {
}

so I am very confused.

Upvotes: 1

Views: 272

Answers (1)

Mitch
Mitch

Reputation: 3418

When the user presses enter they are not entering a NULL character, they are entering a newline. Try checking for \n

Upvotes: 1

Related Questions