Question

#include <stdio.h>
#include <string.h>

/*
    This part is the definition of the structure.
    This contains the attributes the program asks
    from the user of the person the user input
*/
typedef struct {

        char firstName[100];
        char lastName[100];
        char middleName[100];
        char age[100];
        char birthdate[100];
        char address[200];
        char gender[100];
        int numFriends;
} person;

void menu (); // declaring the function for the menu

int main () {

    person inputPerson; //this will be the selected person the user wants to input

    char selectMenu[2];

    /*
        The program will continue asking the user the attributes of the person he/she just input
    */
    printf("Enter the person's first name: ", *inputPerson.firstName);
    gets(inputPerson.firstName);
    printf("Enter the person's last name: ", *inputPerson.lastName);
    gets(inputPerson.lastName);
    printf("Enter the person's middle name: ", *inputPerson.middleName);
    gets(inputPerson.middleName);
    printf("Enter the person's age: ", *inputPerson.age);
    gets(inputPerson.age);
    printf("Enter the person's birthdate: ", *inputPerson.birthdate);
    gets(inputPerson.birthdate);
    printf("Enter the person's home address: ", *inputPerson.address);
    gets(inputPerson.address);
    printf("Enter the person's gender(Male or Female): ", *inputPerson.gender);
    gets(inputPerson.gender);
    printf("How many friends does the person have? ");
    scanf("%d", &inputPerson.numFriends);

    menu (); //this will display the menu to the user

    /*
        This loop is for the selection of menu.
        This will keep looping until the user has input 'X' or 'x'
    */
    while(*selectMenu != 'x' || *selectMenu != 'X') {

        printf("Enter your menu selection: ");

        gets(selectMenu);

        if(*selectMenu == 'a' || *selectMenu == 'A') {

            printf("\n  First name: %s\n\n", inputPerson.firstName);
        }

        if(*selectMenu == 'b' || *selectMenu == 'B') {

            printf("\n  Last name: %s\n\n", inputPerson.lastName);
        }

        if(*selectMenu == 'c' || *selectMenu =='C') {

            printf("\n  Middle name: %s\n\n", inputPerson.middleName);
        }

        if(*selectMenu == 'd' || *selectMenu == 'D') {

            printf("\n  Age: %s\n\n", inputPerson.age);
        }

        if(*selectMenu == 'e' || *selectMenu == 'E') {

            printf("\n  Birthdate: %s\n\n", inputPerson.birthdate);
        }

        if(*selectMenu == 'f' || *selectMenu == 'F') {

            printf("\n  Home Address: %s\n\n", inputPerson.address);
        }

        if(*selectMenu == 'g' || *selectMenu == 'G') {

            printf("\n  Gender: %s\n\n", inputPerson.gender);
        }

        if(*selectMenu == 'h' || *selectMenu == 'H') {

            printf("\n   Number of friends: %d\n\n", inputPerson.numFriends);
        }
    }
}

void menu () {

    /*
        These are the set of command keys given to the user
    */
    printf("\n\n\n");
    printf("PRESS:\n\n");
    printf("         A - Display the person's first name.\n");
    printf("         B - Dispaly the person's last name.\n");
    printf("         C - Display the person's middle name.\n");
    printf("         D - Display the person's age.\n");
    printf("         E - Display the person's address.\n");
    printf("         F - Display the person's gender.\n");
    printf("         H - Display the number of friends the person has.\n");
    printf("         I - Display a specific friend's information.\n");
    printf("         J - Display all information of all friends.\n");
    printf("\n");
    printf("         M - Display the menu.\n");
    printf("         X - Exit the program.\n");
    printf("\n\n");
}

This is my code. What I want to happen is this. If the input in numFriends > 0, the user will have to input the same attributes but under a different variable name(which I do not know how). I'm expecting to use a recursion but I don't know how to use it together with inputting the new attributes. A little help please. Thank you! :D

Was it helpful?

Solution

You could do something like

person *firends = NULL;

if (inputPerson.numFriends) {
    friends = malloc(inputPerson.numFriends * sizeof(*friends));
    for (int i = 0; i < inputPerson.numFriends; i++) {
        printf("Enter the person's first name: ");
        gets(friends[i].firstName);
        ...
    }
}

And use this firends later

if (firends) {
    ...
}

Note: gets() is quite dangerous, you should replace it with safer fgets().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top