#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");
}
这是我的代码。我想要发生的是这个。如果在numFriends中输入&gt; 0,用户必须输入相同的属性,但是在不同的变量名下(我不知道如何)。我期望使用递归,但我不知道如何与输入新属性一起使用它。请帮助一下。谢谢! :d
答案 0 :(得分:1)
您可以执行类似
的操作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);
...
}
}
稍后使用此firends
if (firends) {
...
}
注意:gets()
非常危险,您应该用更安全的fgets()
替换它。