I would like to know why the loop below just execute one time? Here is my code.
for(i=0;i<MAX;i++)
{
printf("name:");
gets(student[i].name);
printf("math score:");
scanf(" %d",student[i].math);
fflush(stdin);
}
I would like to know why the loop below just execute one time? Here is my code.
for(i=0;i<MAX;i++)
{
printf("name:");
gets(student[i].name);
printf("math score:");
scanf(" %d",student[i].math);
fflush(stdin);
}
At least the code contains a typo. You have to write
scanf(" %d", &student[i].math);
^^^
Also this call
fflush(stdin);
has undefined behavior.
And the function gets
is unsafe and is not supported by the C Standard. Instead use function fgets
or scanf
.