0

I am trying to intake a string from user input and then compare that to my linked list that i have created previously in the code and find the location as to where the string should be inserted. And stop looping when user enters nothing and hits enter.

I am able to intake the string and find the location to insert, but what I want to do is to loop until the user enters a blank input. This is causing my code to break somewhere and I am not quite sure why. I have inserted break points in my code to debug it, but I believe I am having issue with fgets. Any help would be amazing.

When I say that the code "breaks", what the output looks like is something like this:

BREAK1: AAAA

BREAK2
BREAK4
               AAAA
   0
BREAK5

The string and the position are correct, but it is printing on multiple lines, and then after this, it continues to loop without resetting. Below is my code::

// NO FILE, SO INTAKE STRINGS
///////////////////////////////////////////////////
///////////////////////////////////////////////////
else{
    fgets(buff,BUFF_SIZE,stdin);
    buff[strlen(buff)] = '\0';

    while (buff[0] != '\0'){
        printf("BREAK1: %s\n", buff);
        // set curr = root node
        curr = root;
        printf("BREAK2\n");
        while (curr->next){
            if (strcmp(buff, curr->stringDat) == 1){
                insertPnt++;
                curr = curr->next;
                printf("BREAK3\n");
            }
            else{
                printf("BREAK4\n");
                insert(buff, insertPnt, root);
                printf("%20s   %d\n", buff, insertPnt);
                break;
            }
        }

        // clear buffer
        for (i = 0; i < BUFF_SIZE; i++) {
            buff[i] = 0;
        }
        printf("BREAK5\n");
        // user input
        fgets(buff, BUFF_SIZE, stdin);
        buff[strlen(buff)] = '\0';
        printf("BREAK6\n");
    }
}

**** UPDATED CODE (STILL NOT STOPPING ON BLANK ENTRY) ****

else{
        while (fgets(buff, BUFF_SIZE, stdin) != NULL){
            buff[strlen(buff) - 1] = '\0';
            insertPnt = 1;

            printf("BREAK1: %s\n", buff);
            // set curr = root node
            curr = root;
            printf("BREAK2\n");
            while (curr->next){
                if (strcmp(buff, curr->stringDat) > 0){
                    insertPnt++;
                    curr = curr->next;
                }
                else{
                    insert(buff, insertPnt, root);
                    printf("%-20s   %d\n", buff, insertPnt);
                    // PRINT LINKED LIST
                    print(root);
                    break;
                }
            }

            // clear buffer
            for (i = 0; i < BUFF_SIZE; i++) {
                buff[i] = 0;
            }
            printf("BREAK5\n");

        }
    }
6
  • 1
    Possibly related to your question... After the fgets calls buff[strlen(buff)] is already equal to the string terminator. If you want to remove the possible trailing newline you should be using buff[strlen(buff) - 1] = '\0'. Commented Nov 15, 2016 at 7:18
  • By the way, the correct way to see if fgets succeeded or not is to check if it returned NULL. So a loop like while (fgets(...) != NULL) is usually used. Commented Nov 15, 2016 at 7:20
  • 1
    What, in the documentation for strcmp(), leads you to believe that function it will predictably return 1, specifically in the test of if (strcmp(buff, curr->stringDat) == 1) ? Commented Nov 15, 2016 at 7:20
  • @WhozCraig, good point, I changed my code to be > 0...thank you for pointing this out. Commented Nov 15, 2016 at 7:33
  • @Someprogrammerdude that did indeed help, my only issue now is entering a blank string is not stopping the loop still...see above for updated code Commented Nov 15, 2016 at 7:34

1 Answer 1

2

The string and the position are correct, but it is printing on multiple lines

Because you are not stripping the trailing new-line lefted by fgets:

fgets(buff,BUFF_SIZE,stdin);
buff[strlen(buff)] = '\0'; /* This is a NO-OP */

Change to

char *ptr;
fgets(buff,BUFF_SIZE,stdin);
if (ptr = strchr(buff, '\n')) {
    *ptr = '\0';
}
Sign up to request clarification or add additional context in comments.

Comments

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.