链表里面的对应项读入错误。郁闷。
链表里面的对应项读入异常。。。郁闷。。。
除了名字(NAME)可以正常读入,其他的都是随机值,而且SEX那边也有问题
struct stu_data//学生数据,分别记录学号,姓名,性别,年龄
{
long num;
char name[NAMELEN];
char sex;
int age;
struct stu_data *next;
};
struct stu_data *Crt(void)//创建链表
{
struct stu_data *head,*bk,*fwd;
head=fwd=bk=(struct stu_data*)malloc(LEN);
//head不存东西,从head-> next开始存
fwd=head-> next=(struct stu_data*)malloc(LEN);
printf( "Now input the number,name,sex and age in order please\n ");
printf( "You can input 0 as a number to cease\n ");
while(1) //如果输入学号为0就BREAK
{
printf( "Num => ");
scanf( "%ld ",fwd-> num);
if (fwd-> num==0)
{
bk-> next=NULL;
free(fwd);
break;
}
printf( "Name => ");
scanf( "%s ",fwd-> name);
printf( "sex => ");
scanf( "%c%c ",fwd-> sex);
printf( "age => ");
scanf( "%d ",fwd-> age);
bk=fwd;
fwd=fwd-> next=(struct stu_data*)malloc(LEN);
}
return (head);
}
------解决方案--------------------
ding
异常处是
scanf( "%ld ",fwd-> num);吗?
------解决方案--------------------
#include <malloc.h>
#include <iostream >
using namespace std;
struct stu_data//学生数据,分别记录学号,姓名,性别,年龄
{
long num;
char *name ;
char sex;
int age;
struct stu_data *next;
};
struct stu_data *Crt(void)//创建链表
{
struct stu_data *head,*bk,*fwd;
head=fwd=bk=(struct stu_data*)malloc(sizeof(stu_data));
//head不存东西,从head-> next开始存
fwd=head-> next=(struct stu_data*)malloc(sizeof(stu_data));
cout < < "Now input the number,name,sex and age in order please\n " ;
cout < < "You can input 0 as a number to cease\n " ;
while(1) //如果输入学号为0就BREAK
{
cout < < "Num => " ;
cin> > fwd-> num ;
if (fwd-> num==0)
{
bk-> next=NULL;
free(fwd);
break;
}
cout < < "Name => " ;
cin> > fwd-> name ;
cout < < "sex => " ;
cin> > fwd-> sex ;
cout < < "age => " ;
cin> > fwd-> age ;
bk=fwd;
fwd=fwd-> next=(struct stu_data*)malloc(sizeof(stu_data));
}
return (head);
};
除了名字(NAME)可以正常读入,其他的都是随机值,而且SEX那边也有问题
struct stu_data//学生数据,分别记录学号,姓名,性别,年龄
{
long num;
char name[NAMELEN];
char sex;
int age;
struct stu_data *next;
};
struct stu_data *Crt(void)//创建链表
{
struct stu_data *head,*bk,*fwd;
head=fwd=bk=(struct stu_data*)malloc(LEN);
//head不存东西,从head-> next开始存
fwd=head-> next=(struct stu_data*)malloc(LEN);
printf( "Now input the number,name,sex and age in order please\n ");
printf( "You can input 0 as a number to cease\n ");
while(1) //如果输入学号为0就BREAK
{
printf( "Num => ");
scanf( "%ld ",fwd-> num);
if (fwd-> num==0)
{
bk-> next=NULL;
free(fwd);
break;
}
printf( "Name => ");
scanf( "%s ",fwd-> name);
printf( "sex => ");
scanf( "%c%c ",fwd-> sex);
printf( "age => ");
scanf( "%d ",fwd-> age);
bk=fwd;
fwd=fwd-> next=(struct stu_data*)malloc(LEN);
}
return (head);
}
------解决方案--------------------
ding
异常处是
scanf( "%ld ",fwd-> num);吗?
------解决方案--------------------
#include <malloc.h>
#include <iostream >
using namespace std;
struct stu_data//学生数据,分别记录学号,姓名,性别,年龄
{
long num;
char *name ;
char sex;
int age;
struct stu_data *next;
};
struct stu_data *Crt(void)//创建链表
{
struct stu_data *head,*bk,*fwd;
head=fwd=bk=(struct stu_data*)malloc(sizeof(stu_data));
//head不存东西,从head-> next开始存
fwd=head-> next=(struct stu_data*)malloc(sizeof(stu_data));
cout < < "Now input the number,name,sex and age in order please\n " ;
cout < < "You can input 0 as a number to cease\n " ;
while(1) //如果输入学号为0就BREAK
{
cout < < "Num => " ;
cin> > fwd-> num ;
if (fwd-> num==0)
{
bk-> next=NULL;
free(fwd);
break;
}
cout < < "Name => " ;
cin> > fwd-> name ;
cout < < "sex => " ;
cin> > fwd-> sex ;
cout < < "age => " ;
cin> > fwd-> age ;
bk=fwd;
fwd=fwd-> next=(struct stu_data*)malloc(sizeof(stu_data));
}
return (head);
};