这里有一个C语言程序,是记录学生信息的,在运行的时候,我发现输出最后会有一些乱数,怎么改才能把它去掉

#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);
温馨提示:答案为网友推荐,仅供参考
第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"水平制表符来隔开,这样就很整齐了

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网