Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
#include <stdio.h>
struct subjectRecord
{
    char subjectCode;
    char subjectName;
    char lecturer;
    int creditHour;
}subject[3];
int main(void)
{
    int a;
    for(a = 0; a <3; 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 <3; 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 find where the error in my code is, as when I try to run the program an error message is displayed as follows: stopped working and close down
Posted

Your subjectCode. subjectName and lecturer variables are declared as characters but you are using them as character arrays. Declare them as character arrays instead of just characters.
 
Share this answer
 
Comments
Jayfam 3-May-11 14:47pm    
i declared them using struct char already
Ed Nutting 3-May-11 14:54pm    
Just seen your new question. If you look at my original solution you will see where your problem lies. Char is one character in a string like 'a' or 'z' or any other letter/number/chrarcter. This means that char subjectName can only hold one character (or in your case letter). What you need in C is to declare a char array which means an array of characters or as you may know it, a string. The code should therefore look like: char *variable name*[*expacted or known length string*] which in your case will probably be char subjectName[100]; That help?

Note: You also haven't changed your formatting in your second for loop. You need to change the %d to %s in subject name print line and %s to %d in the credit hour print line.
Jayfam 3-May-11 14:55pm    
ok, i try it out see
C#
#include <stdio.h>
struct subjectRecord
{
    char subjectCode[20];
    char subjectName[50];
    char lecturer[100];
    int creditHour;
}subject[10];
int main(void)
{
    int a;
    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("\nSubject 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);
    }
}
 
Share this answer
 

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