2014-03-19 20 views
-1
#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> 0中的輸入,用戶將不得不輸入相同的屬性,但使用不同的變量名稱(我不知道如何)。我期待使用遞歸,但我不知道如何將它與輸入新屬性一起使用。請稍微幫忙。謝謝! :d我想添加一組新的屬性,但我不知道如何

+0

你應該在這個'printf(「輸入人的名字:」,* inputPerson.firstName);''和其他類似的調用'printf()''中刪除第二個參數。 –

回答

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()更換。

相關問題