前面有定义结构体
struct student
{
int num;
char name[10];
int score[3];
};
以下是用3种情况输入一个学生的信息:
第一种:for(i=0;i<1;i++) //name放最后
{
printf("Input the number,name,and three scores of the %dth student:\n",i+1);
scanf("%d,%d,%d,%d,%s",&a[i].num,&a[i].score[0],&a[i].score[1],&a[i].score[2],a[i].name);
}
第二种:for(i=0;i<1;i++) //name不放最后
{
printf("Input the number,name,and three scores of the %dth student:\n",i+1);
scanf("%d,%s",&a[i].num,a[i].name);
scanf("%d",&a[i].score[0]);
scanf("%d",&a[i].score[1]);
scanf("%d",&a[i].score[2]);
}
第三种:for(i=0;i<1;i++) //name不放最后
{
printf("Input the number,name,and three scores of the %dth student:\n",i+1);
scanf("%d,%s,%d,%d,%d",&a[i].num,a[i].name,&a[i].score[0],&a[i].score[0],&a[i].score[0]);
}
打印的话都是用这个语句:for(i=0;i<1;i++)
{
printf("\n输出上面的信息:");
printf("%d,%s,%d,%d,%d",a[i].num,a[i].name,a[i].score[0],a[i].score[1],a[i].score[2]);
}
三种方法分别对应的输出显示情况如下:
但是我最想用的第3种方法,但是后面乱码,请问怎么回事?我猜是scanf的回车键结束引起的,但是不知道它内部怎么读数据的?有大神不?