c - 使用gets函数输入

标签 c

在下面的代码中,当我想输入第二个学生或 >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 常见问题解答:

次要注意事项:

关于c - 使用gets函数输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9134208/

相关文章:

c - 在arduino中通过串口读取整数

c - 打印排序 float 组时出错

C 遍历目录并成对打印条目

c - C中的冒泡排序为零

将 libcurl 中的 HTML 与文件中的文本进行比较

c - 是什么阻止了 mtd 被读取?

c - memchr() 是如何工作的?

c - 如何在 C 中解析 "conflicting types error"?

c - Erlang NIF : Threads locking

c - 打印多维数组