标题:为什么会出现死循环?
取消只看楼主
chen1204019
Rank: 1
来 自:广东
等 级:新手上路
帖 子:93
专家分:0
注 册:2012-12-3
结帖率:90.63%
已结贴  问题点数:20 回复次数:5 
为什么会出现死循环?
看代码
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define N 3
struct student
{
    char number[12];
    char name[10];
    float score[N];
    struct student *next;
};
int main(void)
{
    struct student *creat();
    void print(struct student *head);
    struct student *head;
    head=creat();
    print(head);
    return 0;
}
//创建链表
struct student *creat()
{
    struct student *head;
    struct student *p1, *p2;
    int n, i;
    p1=p2=(struct student *)malloc(sizeof(struct student));
    printf("Please input student's number:");
    scanf("%s", p1->number);
    printf("Please input student's name:");
    scanf("%s", p1->name);
    printf("Please input %d 门 score:", N);
    for(i=0; i<N; i++)
    {
        scanf("%f", &p1->score[i]);
    }
    head=NULL;
    while(p1->number != 0)
    {
        n += 1;
        if(1==n)
        {
            head=p1;
        }
        else
        {
            p2->next=p1;
        }
        p2=p1;
        p1=(struct student *)malloc(sizeof(struct student));
        printf("Please input student's number:");
        scanf("%s", p1->number);
        printf("please input student's name:");
        scanf("%s", p1->name);
        printf("Please input student's %d 门score:", N);
        for(i=0; i<N; i++)
        {
            scanf("%f", &p1->score[i]);
        }
        p2->next=p1;
        p2=p1;
    }
    p2->next=NULL;
    return head;
}
//打印链表
void print(struct student *head)
{
    int i;
    struct student *p1;
    p1=head;
    printf("number\tname\t");
    for(i=0; i<N; i++)
    {
        printf("score%d\t", i+1);
    }
    printf("\n");
    while(p1 != NULL)
    {
        printf("%s%s", p1->number, p1->name);
        for(i=0; i<N; i++)
        {
            printf("%3.1f\t", p1->score[i]);
        }
        printf("\n");
        p1=p1->next;
    }
}

如何修改!
搜索更多相关主题的帖子: color next 
2013-01-12 17:30
chen1204019
Rank: 1
来 自:广东
等 级:新手上路
帖 子:93
专家分:0
注 册:2012-12-3
得分:0 
回复 2楼 peach5460
输入时出现死循环

新手发言,请多指教!
2013-01-12 19:47
chen1204019
Rank: 1
来 自:广东
等 级:新手上路
帖 子:93
专家分:0
注 册:2012-12-3
得分:0 
回复 3楼 chen1204019
就是在char number[10]时,我定义当为0时退出输入,但是当输入0时没反应,出现死循环

新手发言,请多指教!
2013-01-12 19:48
chen1204019
Rank: 1
来 自:广东
等 级:新手上路
帖 子:93
专家分:0
注 册:2012-12-3
得分:0 
回复 2楼 peach5460
这里
while(p1->number != 0)
     {
         n += 1;
         if(1==n)
         {
             head=p1;
         }
         else
         {
             p2->next=p1;
         }
         p2=p1;
         p1=(struct student *)malloc(sizeof(struct student));
         printf("Please input student's number:");
         scanf("%s", p1->number);
         printf("please input student's name:");
         scanf("%s", p1->name);
         printf("Please input student's %d 门score:", N);
         for(i=0; i<N; i++)
         {
             scanf("%f", &p1->score[i]);
         }
         p2->next=p1;
         p2=p1;
循环了, 当number=0时也不退出

新手发言,请多指教!
2013-01-12 19:50
chen1204019
Rank: 1
来 自:广东
等 级:新手上路
帖 子:93
专家分:0
注 册:2012-12-3
得分:0 
回复 6楼 神龙赖了
不行啊

新手发言,请多指教!
2013-01-12 22:21
chen1204019
Rank: 1
来 自:广东
等 级:新手上路
帖 子:93
专家分:0
注 册:2012-12-3
得分:0 
回复 7楼 buptlijun
ok了

新手发言,请多指教!
2013-01-12 22:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-396395-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.707817 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved