C用户输入和链接列表

编程入门 行业动态 更新时间:2024-10-28 14:28:15
本文介绍了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用户输入和链接列表

本文发布于:2023-07-30 00:16:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   用户   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!