Click here to Skip to main content
15,303,572 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>
struct subjectRecord
{
    char subjectCode;
    char subjectName;
    char lecturer;
    int creditHour;
}subject[10];
int main(void)
{
    int a;
    for(a = 1; 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 = 1; a <=10; a++){
    printf("Subject Code :%s\n",subject[a].subjectCode);
        printf("Subject Name: %d %d%d\n", subject[a].subjectName);
    printf("Lecturer Name:%s\n",subject[a].lecturer);
    printf("Credit Hour:%s\n",subject[a].creditHour);
   }
}


Please could someone explain where the error in my code is, as when I try to run the program an error message is displayed as follows:
'Program has got a problem and needs to close.'

[Edited following line to above so it makes sense:
'may i know where is my error because when i accept all the input and show the output, it say need to close got problem ==']
Posted
Updated 3-May-11 7:05am
v3

1 solution

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:

gc
struct subjectRecord
{
    char subjectCode[100]; //Make sure an array of 100 chars is enough space to fit the full string your expecting into. Likewise for the next two variables.
    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 subjectarray. 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:

C
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);

}
//and then:
for(a = 0; a <10; a++)
{
    printf("Subject Code :%s\n",subject[a].subjectCode);
    //printf("Subject Name: %d %d%d\n", subject[a].subjectName); Your other problem here was using %d for a string, it should be the below:
    printf("Subject Name: %s\n", subject[a].subjectName);
    printf("Lecturer Name:%s\n",subject[a].lecturer);
    //printf("Credit Hour:%s\n",subject[a].creditHour); And again %s instead of %d:
    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 :)
   
v2
Comments
Jayfam 3-May-11 13:29pm
   
i correct as you told, but i still got problem and need to close.
Ed Nutting 3-May-11 13:35pm
   
Very sorry I forgot to correct the second for loop. Try the new code now (I improved my solution) ;)
Jayfam 3-May-11 13:40pm
   
the second for loop got any problem?
Ed Nutting 3-May-11 13:41pm
   
If you try copying the second for loop now as I have improved my solution. Previously I had simply copy/pasted your old mistake of starting from 1 and going up to ten without correcting it, but I have fixed that now :)
Jayfam 3-May-11 13:47pm
   
Bro, still the same. stopped working when show the output
Albert Holguin 3-May-11 23:02pm
   
it works, the window just closes quickly because there's no pause at the end. Add a _getch() or something similar at the end.
Jayfam 3-May-11 14:20pm
   
Bro, any solution?
Ed Nutting 3-May-11 14:50pm
   
Sorry had to go out for a while. I will try running your code myself and see if I can sol;ve it then.
Albert Holguin 3-May-11 23:00pm
   
tested it, it works, there's just no pause...
Ed Nutting 4-May-11 2:22am
   
Thanks yes I thought it did. If you see Jayfam's repost you will see why he still had problems - he didn't correct everything I told him too ;P
Jayfam 3-May-11 14:51pm
   
ok, thanks a lot Bro.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900