4264 2021-07-14 14:20 采纳率: 100%
浏览 64
已采纳

为什么这个程序输入一次结束了,有关C语言的

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
 struct stu
{
     char bd;
    char *name;
    struct stu *next;
};
struct stu *cread()
{
    int i=0;
struct stu *head,*p,*q;
    q=p=head=(struct stu *)malloc(sizeof(struct stu));
    if(!head)
    {
        printf("创建失败\n");
        exit(1);    
    }
    head->next=NULL;
    q=head;
    printf("请输入\n");
    scanf("%c%s",&q->bd,q->name);
        printf("%s ",q->name);
    while(q->bd!='\n')
    {
        i++;
        printf("%d ",i);
        p->next=q;
        p=q;
        printf("第%d次输入\n");
        scanf("%c%s",&q->bd,q->name);
    }
    p->next=NULL;
    return head;
}
void print(struct stu *h)
{
    struct stu *head;
    head=h;
    while(head->next!=NULL)
        printf("%s ",head->name);
}
int main()
{
         struct stu *a=cread();
        print(a);
     return 0;
 

}

  • 写回答

3条回答 默认 最新

  • qfl_sdu 2021-07-14 14:25
    关注

    scanf("%c%s",&q->bd,q->name);//这里输入的时候会有一个回车符
    printf("%s ",q->name);
    while(q->bd!='\n')//没有处理上次输入的回车符,所以这里就直接结束了,循环不执行。
    修改方法:在while(q->bd!='\n')上面加一句:getchar();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已采纳回答 7月14日
  • 创建了问题 7月14日