自定义函数部分输入p->num后空格后未判断就继续输入p->name,p->score的值;

 

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int num;
    char name[20];
    int score;
    struct node *next;
};

struct node *creatlist();

int main()
{
    struct node *p,*head;
    int temp_score;
    head=creatlist();
    printf("\n");
    scanf("%d",&temp_score);
    for(p=head;p!=NULL;p=p->next)
    {
        if(p->score>=temp_score);
        {
            printf("%d %s %d\n",p->num,p->name,p->score);
        }
    }
    return 0;
}

struct node *creatlist()
{
    struct node *p,*head;
    head=(struct node *)malloc(sizeof(struct node)); //建立头结点
    head->next=NULL; //将head->next置为空
    while(1)
    {
        p=(struct node *)malloc(sizeof(struct node)); //建立p结点,分配内存空间
        scanf("%d",&p->num);
        if(p->num==0)
        {
            break;
        }
        else
        {
            scanf("%s%d",p->name,&p->score);
            p->next=head->next;
            head->next=p;
        }
    }
    return head->next;
    //输入学生个人信息结束,返回头指针

}

 

#include <stdio.h>
#include <stdlib.h>
 
struct node
{
    int num;
    char name[20];
    int score;
    struct node *next;
};
 
struct node *creatlist();
 
int main()
{
    struct node *p,*head;
    int temp_score;
    head=creatlist();
    printf("\n");
    //scanf("%d",&temp_score);
    for(p=head;p!=NULL;p=p->next)
    {
        if(p->score>=temp_score);
        {
            printf("%d %s %d\n",p->num,p->name,p->score);
        }
    }
    return 0;
}
 
struct node *creatlist()
{
    struct node *p,*head;
    head=(struct node *)malloc(sizeof(struct node)); //建立头结点
    head->next=NULL; //将head->next置为空
    while(1)
    {
        p=(struct node *)malloc(sizeof(struct node)); //建立p结点,分配内存空间
        scanf("%d",&p->num);
        if(p->num==0)
        {
            break;
        }
        else
        {
            scanf("%s%d",p->name,&p->score);
            p->next=head->next;
            head->next=p;
        }
    }
    return head->next;
    //输入学生个人信息结束,返回头指针
 
}
 

你好,你在自定义函数中if判断了p->num为0就break,却是是把自定义函数结束掉了,自定义函数结束后回到主函数,主函数之中下2条执行的代码是   

printf("\n");
scanf("%d",&temp_score);

printf打印换行,但是scanf在你结束后又接收了一次数据,因此导致了输入0结束了自定义函数后还需要输入一次才能真正结束掉。

我把问题所在的scanf给注释掉,功能不影响,你看下能不能解决你的问题,如果解决了你的问题,请采纳,谢谢。

 

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632