I'm an amateur programmer, I've been trying to create a structure with scanf and printf using loops but my code breaks down whenever I try to read for more than 1 variable.
#include<stdio.h>
struct list
{
char name;
int age;
float height;
};
int main()
{
int n=2;
struct list s[n];
for(int i=0;i<=n-1;i++)
{
printf("Enter the name of %d:",i+1);
scanf("%c",&s[i].name);
printf("Enter the age of %d:",i+1);
scanf("%d",&s[i].age);
printf("Enter the height of %d:",i+1);
scanf("%f",&s[i].height);
}
for(int i=0;i<=n-1;i++)
{
printf("Name is %c\n",s[i].name);
printf("Age is %d\n",s[i].age);
printf("Height is %f\n",s[i].height);
}
return 0;
}
OUTPUT
Enter the name of 1:A
Enter the age of 1:25
Enter the height of 1:5.5
Enter the name of 2:Enter the age of 2:24
Enter the height of 2:5.6
Name is A
Age is 25
Height is 5.500000
Name is
Age is 24
Height is 5.600000