1

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.

1
  • input never actually changes inside your loop, so it input[0] != '\0' the first time through the loop then it will be the same the second time and the third and the 710143rd time Commented Sep 20, 2017 at 3:02

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

I changed the \0 to a \n and it still gives me an infinite loop. Still very confused on why this is happening.
what does the scanf statement look like
scanf("%s", &input);
Okay, I will try it now. Thank you for your help and time.
The above code will work, however if the user does not press enter you will have no access to what they inputted, make sure you record it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.