Question

I need to ask the user to input information about a person. the name, age(, height,...

I need to ask the user to input information about a person. the name, age(, height, and the birthdate.

After I get the information I need, I store the information through the struct variables. However, I have a problem with putting all the information into one node and calling the function.

here's the full program in a .zip file http://www.filedropper.com/untitled_86

here are the structures

typedef char NAME[41];

typedef struct date
{
   int month;
   int day;
   int year;
} DATE;

typedef struct person
{
   NAME name;
   int age;
   float height;
   DATE bday;
} PERSON;


typedef struct list
{
    void *data;
    struct list *next;
} LIST;


here is the part of the main method that is relevant to the problem

int main(void)
{
    PERSON *person;

    int num;
    puts("Enter the initial number of records:");
    if (scanf("%d", &num) < 1)
        num = DEF_NUM;

    while (num-- > 0)
    {
        person = (PERSON *) malloc(sizeof(PERSON));
        inputPersonalData(person);
        addPersonalDataToDatabase(person);
    }

here's the method where I ask the user input

LIST *head = NULL, *tail = NULL;

void inputPersonalData(PERSON *person)
{
    printf("enter the name");
    scanf("%s", person->name);
    printf("enter the age");
    scanf("%d", &person->age);
    printf("enter the height");
    scanf("%f", &person->height);
    printf("enter the birthdate");
    scanf("%d/%d/%d", &person->bday.month, &person->bday.day, &person->bday.year);

    printf("%s %d %f %d %d %d", person->name, person->age, person->height, person->bday.month,
           person->bday.day, person->bday.year);

    /*head->data = &person->name;
    printf("%p", head->data);*/

}
void addPersonalDataToDatabase(PERSON *person)
{

    // add(head, tail, person);
    // need help here once I call the function add it always leaves
    // with exit code 11

}

linked list function

void add(LIST **head, LIST **tail, void *data)
{
    if (*tail == NULL)
    {
        *head = *tail = (LIST *) malloc(sizeof(LIST));
        (*head)->data = data;
        (*head)->next = NULL;
    } else
    {
        (*tail)->next = (LIST *) malloc(sizeof(LIST));
        *tail = (*tail)->next;
        (*tail)->data = data;
        (*tail)->next = NULL;
    }
}
 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your code is absolutely fine but since your add function in list.c is taking a double pointer for head and tail of struct LIST. You need to pass the address of head and tail, which you have declared and defined in person.c file, to add() function but you were just passing the value of head and tail to add() function that is why you were getting a runtime error.

Call add function like this:-

void addPersonalDataToDatabase(PERSON *person)
{
add(&head, &tail, person);

}

I have tested your code after correction and it is not producing any runtime error.

SAMPLE OUTPUT:

IF YOU STILL NEED ANY HELP THEN REACH ME OUT IN COMMENTS

Add a comment
Know the answer?
Add Answer to:
I need to ask the user to input information about a person. the name, age(, height,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT