我想练习一下结构体,便写出以下程序,结果与我想象中的大不相同,请指点一二
源程序:
#include <stdio.h>
#define SIZE 100
struct student {
char name;
int result;
};
void getStudent(struct student a[]);
void putStudent(struct student a[]);
int main()
{
struct student a[SIZE];
getStudent(a);
printf("\n");
printf("\n");
printf("\n");
printf("\n");
putStudent(a);
return 0;
}
void getStudent(struct student a[])
{
int i;
for(i=0; i<5; i++){
printf("%s","Enter Name: ");
scanf("%c",&a[i].name);
printf("\n");
printf("%s","Enter Result: ");
scanf("%d",&a[i].result);
}
}
void putStudent(struct student a[])
{
int i;
for(i=0; i<5; i++){
printf("Name: %c Result: %d\n",a[i].name,a[i].result);
}
}
期待结果:
Enter Name: a
Enter Result: 1
Enter Name: b
Enter Result: 2
Enter Name: c
Enter Result: 3
Enter Name: d
Enter Result: 4
Enter Name: e
Enter Result: 5
Name: a Result: 1
Name: b Result: 2
Name: c Result: 3
Name: d Result: 4
Name: e Result: 5
但实际的程序执行结果:
Enter Name: a
Enter Result: 1
Enter Name: ------------------------不能输入
Enter Result: 2
Enter Name: ------------------------不能输入
Enter Result: 3
Enter Name: ------------------------不能输入
Enter Result: 4
Enter Name: ------------------------不能输入
Enter Result: 5
Name: a Result: 1
Name:---------------------------------------------
Result: 2 --------------------这两行本应该在同一行的.
Name:
Result: 3
Name:
Result: 4
Name:
Result: 5
请问:为什么会有这样的结果,在"------------------------不能输入"的地方不是应该能输入的吗?在for循环里面为什么会有错呢?在"-----------------------------------" 与 "--------------------这两行本应该在同一行的"的地方为什么不在同一行,而分别显示在两行呢?
谢谢!