| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 482 人关注过本帖
标题:[求助]结构体数组遇到的问题
只看楼主 加入收藏
lg_mic
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2007-9-18
收藏
 问题点数:0 回复次数:5 
[求助]结构体数组遇到的问题

这是书上的例子,代码如下

/*6.14结构体,输入两个学生的信息并打印出来*/
#define MAX 30
typedef struct datatype{
int year;
char month[12];
int date;
}DATE;
typedef struct personnel{
char name[20];
char sex[8];
DATE birthday;
char speciality[20];
char class1[10];
}PERSONNEL; /*typedef struct personnel PERSONNEL;错误的例子*/
PERSONNEL student[MAX];
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i,total;
printf("input the total number of students:");
scanf("%d",&total);
for(i=0;i<total;i++)
{
printf("input the %d student's name,sex:",i);
scanf("%s,%s",student[i].name,student[i].sex);
printf("input the year month,date of %d student's birthday:",i);
scanf("%d,%s,%d",student[i].birthday.year,student[i].birthday.month,student[i].birthday.date);
printf("input the speciality,class of %d student:",i);
scanf("%s,%s",student[i].speciality,student[i].class1);
}
for(i=0;i<total;i++)
{
printf("|%2d|%-6s",i,student[i].name);
printf("|%2s",i,student[i].sex);
printf("|%4d",i,student[i].birthday.year);
printf("|%2s",i,student[i].birthday.month);
printf("|%2d",i,student[i].birthday.date);
printf("|%12s",i,student[i].speciality);
printf("|%8s|\n",i,student[i].class1);
}
system("pause");
return 0;
}

运行时在红字处出现错误,程序直接跳出。到底是那里错了?
搜索更多相关主题的帖子: 结构体 
2007-09-23 16:13
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
scanf("%d,%d,%s",student[i].birthday.year,student[i].birthday.date,student[i].birthday.month);
试下.

倚天照海花无数,流水高山心自知。
2007-09-23 19:06
lg_mic
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2007-9-18
收藏
得分:0 
没有解决,问题依旧。

2007-09-23 19:32
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
以下是引用lg_mic在2007-9-23 16:13:54的发言:

这是书上的例子,代码如下

/*6.14结构体,输入两个学生的信息并打印出来*/
#define MAX 30
typedef struct datatype{
int year;
char month[12];
int date;
}DATE;
typedef struct personnel{
char name[20];
char sex[8];
DATE birthday;
char speciality[20];
char class1[10];
}PERSONNEL; /*typedef struct personnel PERSONNEL;错误的例子*/
PERSONNEL student[MAX];
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i,total;
printf("input the total number of students:");
scanf("%d",&total);
for(i=0;i<total;i++)
{
printf("input the %d student's name,sex:",i);
scanf("%s,%s",student[i].name,student[i].sex);
printf("input the year month,date of %d student's birthday:",i);
scanf("%d,%s,%d",student[i].birthday.year,student[i].birthday.month,student[i].birthday.date);
printf("input the speciality,class of %d student:",i);
scanf("%s,%s",student[i].speciality,student[i].class1);
}
for(i=0;i<total;i++)
{
printf("|%2d|%-6s",i,student[i].name);
printf("|%2s",i,student[i].sex);
printf("|%4d",i,student[i].birthday.year);
printf("|%2s",i,student[i].birthday.month);
printf("|%2d",i,student[i].birthday.date);
printf("|%12s",i,student[i].speciality);
printf("|%8s|\n",i,student[i].class1);
}
system("pause");
return 0;
}

运行时在红字处出现错误,程序直接跳出。到底是那里错了?


倚天照海花无数,流水高山心自知。
2007-09-23 19:39
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

输入时要加,

建议日期全部用整型.加个日期判断函数.如果真的需要把月份改成英文就定义一个数组保存月份名称.

下面这个应该可以了吧:

#define MAX 30
#include<stdio.h>
#include<stdlib.h>
typedef struct datatype{
int year;
int month;
int date;
}DATE;
typedef struct personnel{
char name[20];
char sex[8];
DATE birthday;
char speciality[20];
char class1[10];
}PERSONNEL; /*typedef struct personnel PERSONNEL;错误的例子*/
PERSONNEL student[MAX];

int main(void)
{
int i,total;
printf("input the total number of students:");
scanf("%d",&total);
for(i=0;i<total;i++)
{
printf("input the %d student's name,sex:",i);
scanf("%s%s",student[i].name,student[i].sex);
printf("input the year month,date of %d student's birthday:",i);
scanf("%d%d%d",&student[i].birthday.year,&student[i].birthday.month,&student[i].birthday.date);
printf("input the speciality,class of %d student:",i);
scanf("%s%s",student[i].speciality,student[i].class1);
}
for(i=0;i<total;i++)
{
printf("|%2d|%-6s",i,student[i].name);
printf("|%2s",i,student[i].sex);
printf("|%4d",i,student[i].birthday.year);
printf("|%2s",i,student[i].birthday.month);
printf("|%2d",i,student[i].birthday.date);
printf("|%12s",i,student[i].speciality);
printf("|%8s|\n",i,student[i].class1);
}
return 0;
}


倚天照海花无数,流水高山心自知。
2007-09-23 19:43
lg_mic
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2007-9-18
收藏
得分:0 
回复:(nuciewth)输入时要加,建议日期全部用整型.加...

好了,没有问题了,多谢。我之前的代码在最后的printf那里也有问题,我重修改一下再发上来。

#define MAX 30
#include<stdio.h>
#include<stdlib.h>
typedef struct datatype{
int year;
int month;
int date;
}DATE;
typedef struct personnel{
char name[20];
char sex[8];
DATE birthday;
char speciality[20];
char class1[10];
}PERSONNEL; /*typedef struct personnel PERSONNEL;错误的例子*/
PERSONNEL student[MAX];

int main(void)
{
int i,total;
printf("input the total number of students:");
scanf("%d",&total);
for(i=0;i<total;i++)
{
printf("input the %d student's name,sex:",i);
scanf("%s%s",student[i].name,student[i].sex);
printf("input the year month,date of %d student's birthday:",i);
scanf("%d%d%d",&student[i].birthday.year,&student[i].birthday.month,&student[i].birthday.date);
printf("input the speciality,class of %d student:",i);
scanf("%s%s",student[i].speciality,student[i].class1);
}
for(i=0;i<total;i++)
{
printf("|%2d|%-6s",i,student[i].name);
printf("|%6s",student[i].sex);
printf("|%4d",student[i].birthday.year);
printf("|%2d",student[i].birthday.month);
printf("|%2d",student[i].birthday.date);
printf("|%12s",student[i].speciality);
printf("|%8s|\n",student[i].class1);
}
system("pause");
return 0;
}


关于你所说的输入时要加','的问题,我确实有加啊。依然是异常跳出,结构体中同时包含int和char型的话会出现内存分配问题?全改成int就没事了,怪哉。请问你知不知道为什么会出现这样的问题,和类似问题的解决方法呢?

2007-09-23 20:36
快速回复:[求助]结构体数组遇到的问题
数据加载中...
 
   



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

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