我的问题看起来很简单,我很抱歉询问,但是这段代码有什么问题呢?为什么只跳过名称部分?
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define nl printf("\n")
struct date{int day,month,year;};
struct student{long int id;char name[30];struct date birthday;};
int main()
{
struct student temp;
nl;nl;printf("ID no:");scanf("%ld",&temp.id);nl;
printf("Student name:");
gets(temp.name);
nl;nl;
printf("Student birthday year:19");scanf("%d",&temp.birthday.year);nl;
printf("Student birthday month");scanf("%d",&temp.birthday.month);nl;
printf("Student birthday day");scanf("%d",&temp.birthday.day);nl;
getch(); //for pause
return 0;
}
获取功能有什么问题吗?scanf("%s",)
因为空间的事我不想使用...
这是因为它读取了\n
留下的字符scanf
。利用
int ch;
while((ch = getchar()) != '\n' && ch != EOF);
消耗\n
。
最好不要使用gets
它,因为它在数组绑定检查中失败。使用fgets
代替。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句