samedi 21 février 2015

Trying to print a struct element but getting blank. - C

I have a struct person that has the following elements, defined in data.h



typedef struct person{
char firstName[20];
char familyName[20];
char telephoneNum[20];
int type; // 0 = student / 1 = employee;
}newPerson;


I created an array of person[MAX_PERSONS] that is initialized in my menu() function. I then have an addFirstName(newPerson pers) function. However when I try to test print format using my printFormat(newPerson pers)function, I get blank, instead of the inputted name.


I have included the menu(), addFirstname(newPerson pers), and printFormat(newPerson pers) function below. I was wondering if anyone could tell me the reason for this. Any help or pointers would be appreciated. Thank you in advance.



int menu(){
int num = 0;
newPerson person[MAX_PERSONS];
int option; // for user input for menu
printf("\n\tPlease choose one of the following options to continue (0-9): ");
scanf("%d", &option );
printf("\n\tYou selected %d\n", option);

if (option == 0){ //program will close
printf("\tProgram will now close.\n");
exit(1);
}

if (option == 1){ //program will ask for name input
addRecord(person[num]);
printFormat(person[num]);
char choice[0];
printf("\n\t\tWould you like to enter another record? (y/n): ");
scanf("%s", choice);
if (choice[0] == 'y'){
num++;
addRecord(person[num]);

}
if (choice[0] == 'n'){
num++;
mainMenu();
}
/*

IF YES, THEN NUM++
THEN RUN ADDRECORD(PERSONNUM) AGAIN.

IF NO, THEN RETURN TO MAIN MENU.
PRINTMENU
THEN RUN MENU AGAIN
*/
}

printf("\n\tNot a valid option, please try again // THE END OF MENU FUNCTION\n");
return 0;
}

void addFirstName(newPerson pers){
char firstName[20];
printf("\n\tEnter first Name: ");
scanf("%20s", firstName);
strcpy(pers.firstName, firstName);
printf("\n\tThe name entered is %s", pers.firstName);
}


void printFormat(newPerson pers){
printf("\t\tThe name is %s", pers.firstName);
}

Aucun commentaire:

Enregistrer un commentaire