Can someone tell me what is wrong here. I want to loop the array and print each student data on a single line, but instead I get each member data on a separate line.
Enter first name for student 1: James
Enter last name for student 1: Wilson
Enter age of student: 25
Enter first name for student 2: Jamie
Enter last name for student 2: Wilson
Enter age of student: 22
You entered:
James
Wilson
25
Jamie
Wilson
22
Code:
#include <stdio.h>
int main()
{
struct person {
char firstName[11];
char lastName[16];
int age;
};
int count=2,i,ch;
struct person student[count];
for( i=0; i < count; i++)
{
printf("Enter first name for student %d: ", i + 1);
fgets(student[i].firstName,10,stdin);
printf("Enter last name for student %d: ", i +1);
fgets(student[i].lastName,15,stdin);
printf("Enter age of student: ");
fscanf(stdin," %d", &student[i].age);
ch=getchar();
}
printf("You entered: \n");
for( i=0; i < count; i++)
{
printf("%s %s %d\n", student[i].firstName, student[i].lastName,student[i].age);
}
return 0;
}