C用户输入和链接列表 [英] C user input and Linked List

查看:61
本文介绍了C用户输入和链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从用户输入中提取一个字符串,然后将其与我先前在代码中创建的链接列表进行比较,并找到应在何处插入字符串的位置.当用户什么都不输入然后按回车键时,就停止循环.

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.

我能够提取字符串并找到要插入的位置,但是我要做的是循环播放,直到用户输入空白输入为止.这导致我的代码在某处中断,我不太清楚为什么.我在代码中插入了断点以对其进行调试,但是我相信fgets存在问题.任何帮助都将是惊人的.

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

        }
    }

推荐答案

字符串和位置正确,但是正在打印 多行

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

因为您没有剥离fgets留下的尾随换行符:

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 */

更改为

char *ptr;
fgets(buff,BUFF_SIZE,stdin);
if (ptr = strchr(buff, '\n')) {
    *ptr = '\0';
}

这篇关于C用户输入和链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆