在下面的代码中,当我想输入第二个学生或 >2 个学生的记录时.. 编译器跳过名称输入并接受类(class)和年龄的输入.. 有什么问题请帮帮我?
#include <stdio.h>
#include <conio.h>
struct Student
{
char Name[16];
char Class[16];
int age ;
};
void main()
{
struct Student a[5] ;
for(int i=0 ; i<5 ; i++)
{
printf("\n Enter Name :");
gets(a[i].Name);
printf("\n Enter Class :");
gets(a[i].Class);
printf("\n Enter Age : ");
scanf("%d" , & a[i].age);
}
getch();
}
最佳答案
这个问题很常见。试试这个:
scanf("%d ", &a[i].age);
^ <--- This space will make scanf eat the remaining blanks
有关于此的 C 常见问题解答:
次要注意事项:
- Don't use
gets
强>。使用fgets
相反。
关于c - 使用gets函数输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9134208/