鱼C论坛

 找回密码
 立即注册
查看: 1150|回复: 5

[已解决]关于链表的一个问题

[复制链接]
发表于 2021-3-15 22:56:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
对于上次的帖子忘贴代码感到十分羞愧 这次重新发一个帖子 希望路过的大佬能帮帮我 程序使用vs2019写的

void input_print()     //输入与输出的函数
{
        struct node* p1, * p2, * head;
        int n, temp;
        char temp_2;
        p1 = p2 = (struct node*)malloc(sizeof(struct node));

        printf("Plese input the book name:");
        scanf("%s", &p1->name);
        printf("Plese input the book author:");
        scanf("%s", &p1->author);

        head = NULL;
        n = 0;

        while (1)
        {
                n++;

                if (n == 1)
                {
                        head = p1;
                }
                else
                {
                        p2->next = p1;
                }
                p2 = p1;

                p1 = (struct node*)malloc(sizeof(struct node));

                printf("Do you want to input information again? Y/N");
                scanf("%s", &temp_2);
                if (temp_2 == 'n' || temp_2 == 'N')
                {
                        break;
                }


                printf("Plese input the book name:");
                scanf("%s", &p1->name);
                printf("Plese input the book author:");
                scanf("%s", &p1->author);

        }
        p1 = NULL;

        while (head != NULL)
        {

                
                for (temp = 1; temp <= n; temp++)
                {
                        printf("第%d本书", temp);
                        printf("书名:《%s》\n", head->name);
                        printf("作者: %s", head->author);
                        printf("\n \n");
                        
                        head = head->next;
                }
                
                head = NULL;
        }
        
}     //这里出现"Run-Time Check Failure #2 - Stack around the variable 'temp_2' was corrupted."错误提示

        这是node结构体的内容
struct node       
{
        char name[50];
        char author[50];
        struct node* next;
};

最佳答案
2021-3-15 23:31:46
本帖最后由 jackz007 于 2021-3-15 23:33 编辑
        char temp_2;
. . . . . .
        while (1)
        {
. . . . . .
                scanf("%s", & temp_2)  ; // char 型的变量不可以这样获取键盘输入
        正确的方法
                fflush(stdin)          ; // 添加此句,读取单个字符之前,需要清空键盘缓冲区
                scanf("%c" , & temp_2) ; // char 型的变量应该这样获取键盘输入
        也就是说,把原来的 scanf() 那一句改成上面的那两句就可以了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-15 23:23:03 From FishC Mobile | 显示全部楼层
本帖最后由 shiwobuhaoma 于 2021-3-15 23:42 编辑

光申请内存了,没有释放内存操作,还有如果加上申请内存后检查是否为空会更严谨一点。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-15 23:24:19 From FishC Mobile | 显示全部楼层
本帖最后由 shiwobuhaoma 于 2021-3-15 23:42 编辑

1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-15 23:25:55 From FishC Mobile | 显示全部楼层
temp2是char类型,你用%s不对吧,应该%c
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-15 23:31:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-3-15 23:33 编辑
        char temp_2;
. . . . . .
        while (1)
        {
. . . . . .
                scanf("%s", & temp_2)  ; // char 型的变量不可以这样获取键盘输入
        正确的方法
                fflush(stdin)          ; // 添加此句,读取单个字符之前,需要清空键盘缓冲区
                scanf("%c" , & temp_2) ; // char 型的变量应该这样获取键盘输入
        也就是说,把原来的 scanf() 那一句改成上面的那两句就可以了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-16 12:55:53 | 显示全部楼层
啊这 原来是%c啊 一直以为是%s 误记了 谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 05:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表