问题描述
当我在循环中使用fgets时,它将前一个工作的enter命令作为输入并对其进行处理,这不仅发生在fgets中,而且在许多其他情况下也发生在我身上,所以我的问题是....
1。是否可以在开始任何循环时停止前面的工作命令?
2。如果不是,请告诉我如何解决此问题?
程序
#include <stdio.h>
#include <strings.h>
struct student
{
char name[10];
}st[10];
int main()
{
int i,j;
printf("enter the number of students=");
scanf("%d",&i);
j=i;
for (i=1;i<=j;i++)
{
printf("enter the student's name= ");
fgets(st[i].name,sizeof(st[i]),stdin);
printf("%s",st[i].name);
}
}
输出
enter the number of students=2
enter the student's name=
enter the student's name= hello
hello
请参阅此处,它从第二行开始输入,即,将第一输入作为回车键并移至第二输入
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)