|
5鱼币
#include<stdio.h>
int main()
{
struct S
{
int a;
int b;
char c[20]; //当这里为数组时,、、//当c[20]改为*c;
struct S *next;
};
struct S p1,p2,*head;
printf("输入学生名\n");
gets(p1.c); //这里成功输入“小明”,//输入过后程序就直接崩溃了,为什么?
printf("请输入数学成绩\n");
scanf("%d",&p1.a);
printf("请输入语文成绩\n");
scanf("%d",&p1.b);
printf("输入学生名\n");
gets(p2.c); //这里就直接跳过了,没让我输入。这是为什么?
printf("请输入数学成绩\n");
scanf("%d",&p2.a);
printf("请输入语文成绩\n");
scanf("%d",&p2.b);
head=&p1;
p1.next=&p2;
p2.next=NULL;
while(head)
{
printf("%s %d %d\n",head->c,head->a,head->b);
head=head->next;
}
}
|
最佳答案
查看完整内容
楼主看这句话 这句话的意思是说
gets函数会将标准输入内的第一个换行字符也包含进来, 言外之意就是默认为当出现第一个回车字符时, gets函数便终止
而当执行scanf("%d",&p1.b);
标准输入内除了b的数据, 还跟着一个回车字符, 二回车字符是啥, 就是\n (虽然是\r\n, 这个先不说)
所以换句话说, 虽然把标准输入的数据送到p1.b的内存内, 但是在标准输入内还驻留着一个不干净的'\n'字符
所以利用fflush(stdin), 将标准输入 ...
|