回 帖 发 新 帖 刷新版面

主题:运行时,这个循环终止不了.

#include<stdio.h>
#include<string.h>

struct student
{
    int number;//学号
    char name[20];//姓名
    int age;//年龄
    
    struct student* next;
};
struct student* greatlist();
struct student* showlist (struct student* head);
void main(void)
{
    student* head = NULL;
    head = greatlist();
    showlist(head);
    return;
}
struct student* greatlist()
{
    student* head = NULL;
    student* tail = NULL;
    student* s = NULL;
    while(1)
    {
        int Number;
        int Aage;
        char nName[20];
        printf("请输入学号:");
        scanf("%d",&Number);
        printf("姓名:");
        scanf("%d",nName);
        fflush(stdin);
        printf("年龄:");
        scanf("%s",&Aage);
        if(Number<=0)
        {
            break;
        }
        s = new student;
        //    s->name=nName;
        strcpy(s->name,nName);
        s->age = Aage;
        s->number = Number;
        s->next = NULL;
        if(head == NULL)
        {
            head = tail = s;
        }
        else
        {
            tail->next = s;
            tail = s;
        }
    }
    return head;
}
struct student* showlist (struct student* head)
{
    student* s1 = head;
    while(s1 != NULL)
    {
        printf("姓名          年龄");
        printf("%s%d",s1->name,s1->name);
        s1 = s1->next;
    }
    printf("NULL\n");
    return head;
}

回复列表 (共3个回复)

沙发

请问一下这个程序是不是一直在输入姓名,学号等输入过程中运行啊?我好久没用C语言了,突然看到这个程序,感觉有点陌生!!!

板凳

你的whlie循环是个永真式
只要输入的Number不小于0
就不会跳出循环

3 楼


这程序是下面显示写错了,改过来了,正确答题:
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
struct student
{
    int number;//学号
    char name[20];//姓名
    int age;//年龄
    
    struct student* next;
};
struct student* greatlist();
struct student* showlist (struct student* head);
void headnumber();
void main(void)
{
    
    list();
    void headnumber();
    student* head = NULL;
    head = greatlist();
    showlist(head);
    return;
}

//显示菜单函数

void list()
{
    while(1)
    {
        int n;
        printf("\n");
        printf("\t********************************************\n");
        printf("\t*                                          *\n");
        printf("\t*                                          *\n");
        printf("\t*          1.建立学生信息文件              *\n");
        printf("\t*          2.删除学生信息记录              *\n");
        printf("\t*          3.修改学生信息记录              *\n");
        printf("\t*          4.读取学生信息记录              *\n");
        printf("\t*          5.谢谢使用,退出查询系统 !       *\n");
        printf("\t*                                          *\n");
        printf("\t*                                          *\n");
        printf("\t********************************************\n");
A:
        printf("\n");
        printf("请选择数字功能号:1---5\n");
        scanf("%d",&n);
        if(n==5)
        {
            break;
        }
        if(n >= 6|| n<=0)
        {
            printf("**您的输入错误,请重新选择数字功能号:1--5**\n");
            goto A;
        }
        
    }
}

struct student* greatlist()
{
    student* head = NULL;
    student* tail = NULL;
    student* s = NULL;
    while(1)
    {
        int Number;
        int Aage;
        char nName[20];
        printf("请输入学号:");
        scanf("%d",&Number);
        if(Number<=0)
        {
            break;
        }
        printf("姓名:");
        scanf("%s",nName);
        fflush(stdin);
        printf("年龄:");
        scanf("%d",&Aage);
        s = new student;
        strcpy(s->name,nName);
        s->age = Aage;
        s->number = Number;
        s->next = NULL;
        if(head == NULL)
        {
            head = tail = s;
        }
        else
        {
            tail->next = s;
            tail = s;
        }
    }
    return head;
}
struct student* showlist (struct student* head)
{
    student* s1 = head;
    while(s1 != NULL)
    {
        printf("\t学  号            姓   名      年   龄\n");
        printf("\t%5d%15s%15d\n",s1->number,s1->name,s1->age);
        s1 = s1->next;
    }
    return head;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册