#include<stdio.h>
struct Date
{ int year,month,da;
};
typedef struct Date Date;
struct Student
{ char name[20];
int scores;
Date dt;
};
typedef struct Student Student;
int main()
{ int i;
Student stu[3];
printf(" Please input scores of 3 students :\n name score year month day \n");
for(i=0;i<3;i++)
scanf("\n%s, %d, %d, %d, %d", &stu[i].name ,&stu[i].scores,&stu[i].dt.year,&stu[i].dt.month,&stu[i].dt.da);
for(i=0;i<3;i++)
printf("%s%d%d%\n",stu[i].name,stu[i].scores,stu[i].dt.year,stu[i].dt.month,stu[i].dt.da);
return 0;
}
你输入的时候出毛病了,
scanf("%s%d%d%d%d",&stu[i].name ,&stu[i].scores,&stu[i].dt.year,&stu[i].dt.month,&stu[i].dt.da);就已经把换行带进去了。如今你写
scanf("\n%s, %d, %d, %d, %d", &stu[i].name ,&stu[i].scores,&stu[i].dt.year,&stu[i].dt.month,&stu[i].dt.da);输入的格式不好控制。你要依次输入“换行”,“,”,两个“空格”,“数字”。。。。。。把输入的所有都重复,否则,会被覆盖,自然输出乱码。
要么,改成上面的,要不,直接分开输入:
scanf("%s" , stu[i].name);
scanf("%d" , &stu[i].scores);
scanf("%d" , &stu[i].dt.year);
scanf("%d" , &stu[i].dt.month);
scanf("%d" , &stu[i].dt.da);
scanf("%s%d%d%d%d",&stu[i].name ,&stu[i].scores,&stu[i].dt.year,&stu[i].dt.month,&stu[i].dt.da);就已经把换行带进去了。如今你写
scanf("\n%s, %d, %d, %d, %d", &stu[i].name ,&stu[i].scores,&stu[i].dt.year,&stu[i].dt.month,&stu[i].dt.da);输入的格式不好控制。你要依次输入“换行”,“,”,两个“空格”,“数字”。。。。。。把输入的所有都重复,否则,会被覆盖,自然输出乱码。
要么,改成上面的,要不,直接分开输入:
scanf("%s" , stu[i].name);
scanf("%d" , &stu[i].scores);
scanf("%d" , &stu[i].dt.year);
scanf("%d" , &stu[i].dt.month);
scanf("%d" , &stu[i].dt.da);
温馨提示:答案为网友推荐,仅供参考
第1个回答 2012-03-15
printf("name\tscore\tyear\tmonth\tday\n");
printf("%s\t%d\t%d\t%d\t%d\n",stu[i].name,stu[i].scores,stu[i].dt.year,stu[i].dt.month,stu[i].dt.da);
使用转义字符"\t"水平制表符来隔开,这样就很整齐了
printf("%s\t%d\t%d\t%d\t%d\n",stu[i].name,stu[i].scores,stu[i].dt.year,stu[i].dt.month,stu[i].dt.da);
使用转义字符"\t"水平制表符来隔开,这样就很整齐了