求教:链表这一章节,,当输入一行,回车就再输入不上,怎么回事
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct Student)
typedef struct Student
{
long num;
float score;
struct Student* next;
}Stu;
int n;
Stu* creat()
{
Stu* head;
Stu* p1, * p2;
n = 0;
p1 = p2 = (struct Student*)malloc(LEN);
scanf("%ld,%f", &p1->num, &p1->score);
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p1 = (Stu*)malloc(LEN);
scanf("%ld,%f", &p1->next, &p1->score);
}
p2->next = NULL;
return head;
}
void print(struct Student* head)
{
struct Student* p;
printf("\nNow,These %d records are:\n", n);
p = head;
if (head != NULL)
{
do
{
printf("%ld %5.1f\n", p->num, p->score);
p = p->next;
} while (p != NULL);
}
}
int main()
{
Stu* head;
head = creat();
print(head);
return 0;
} scanf("%ld,%f", &p1->num, &p1->score);
首先我推荐你不要一次输入多个变量,如果实在想写的简单点
scanf("%ld%*c%f%*c", &p1->num, &p1->score);
%*c 我也是从别人那学来的,我觉得还挺好用
功能就是过滤一个字符这样你两个变量中间就可以随便输什么字符用来隔开两个变量
然后结尾跟上一个 %*c
刚刚说了 %*c 是过滤字符,结尾跟上一个 %*c 就是为了过滤掉你按下的回车
也就是 \n ,如果不过滤的话这个\n 会带到下一次输入
页:
[1]