You appear to have four significant problems two related to memory issues and two with format. The first is that when you read in the subjectCode, subjectName and lecturer strings you try to assign them to chars. This will give you a memory leak as it will overun the memory assigned to one char. The code you should use in your struct should be as follows:
struct subjectRecord
{
char subjectCode[100];
char subjectName[100];
char lecturer[100];
int creditHour;
}subject[10];
The next problem is that oyu try to have a for loop starting at one and going up to ten but using the same variable (
a
) to index into the
subject
array. This is a problem because arrays do not start from index 1 but from index 0, they are 'zero-based'. This means that what you think of as element (or
subjectRecord
) 1 in your
subject
array is actually element 0 and element 10 is element 9, 8 is 7 etc etc. Your
for
loops should therefore look like the following:
for(a = 0; a < 10; a++)
{
printf("\nEnter Subject Code: ");
scanf("%s", &subject[a].subjectCode);
printf("Enter Subject Name: ");
scanf("%s", &subject[a].subjectName);
printf("Enter Lecturer Name: ");
scanf("%s", &subject[a].lecturer);
printf("Enter Credit Hour: ");
scanf("%d", &subject[a].creditHour);
}
for(a = 0; a <10; a++)
{
printf("Subject Code :%s\n",subject[a].subjectCode);
printf("Subject Name: %s\n", subject[a].subjectName);
printf("Lecturer Name:%s\n",subject[a].lecturer);
printf("Credit Hour:%d\n",subject[a].creditHour);
}
Your last two problems would cause probably fatal formatting issues. See the code above for explaination.
Hope this helps,
Ed :)