C user input and Linked List

2019-09-11 05:12发布

问题:

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");

        }
    }

回答1:

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';
}