I have write simple code using structure method and in that code I am taking string(name) as input using for loop for 3 students. For 1st iteration each line in for loop is working as it should be.... but problem comes on second iteration... in second iteration scanf
is skipping input for string (name)...
My code is as below :
#include<stdio.h>
struct student_data
{
char name[5];
int dsd;
int dic;
int total;
};
void main()
{
struct student_data s[3];
int i;
for(i = 0;i<3;i++)
{
printf("Enter Name of Student\n");
scanf("%[^\n]",s[i].name); // Problem occures here in
// second iteration
printf("\nEnter marks of DSD\n");
scanf("%d",&s[i].dsd);
printf("Enter marks of DIC\n");
scanf("%d",&s[i].dic);
s[i].total = s[i].dsd+s[i].dic;
}
printf("%-10s %7s %7s %7s\n ","Name","DSD","DIC","Total");
for(i=0;i<3;i++)
{
printf("%-10s %5d %6d %6d\n",s[i].name,s[i].dsd,s[i].dic,s[i].total);
}
}