#include <stdio.h>
struct student
{
char name[100];
int roll;
};
int main()
{
struct student st[2];
for (int i=0; i<2; i++)
{
char c;
printf ("\nEnter the details of student %d: \n", i+1);
printf ("Enter the name of the student: ");
gets (st[i].name);
printf ("Enter the roll number of the student: ");
scanf ("%d", &st[i].roll);
}
for (int i=0; i<2; i++)
{
printf ("\nThe details of student %d: \n", i+1);
printf ("Name: %s", st[i].name);
printf ("Roll no.: %d", st[i].roll);
}
return 0;
}
The first iteration works as intended, but in the second iteration the fgets() function skips the input and the control moves to the next input statement. I have tried using scanf() to input the name, but I want to be able to take inputs having multiple words.
The output - https://i.stack.imgur.com/OuerT.jpg