I am extremely new to C and struggling a lot. I've looked all over the place and can't find anything that I can understand or that fits my query. My task is to create an array that contains details about 6 people using structures. I then have to create a menu system that allows someone to search gender, first name, last name, date of birth and start date.
This is my .h file:
#define NAME_LENGTH 50
struct strDate
{
int nDay;
int nMnth;
int nYear;
};
struct strPerson
{
char arcFirstName[NAME_LENGTH];
char arcLastName[NAME_LENGTH];
char cSex;
struct strDate strDOB;
struct strDate strStartDate;
};
And this is my .c file (I have taken out all but one of the structure definitions just for the sake of keeping the code to a minimum on here. Elements 1-5 of the array are very similar to element 0, only the actual data varies):
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "Header.h"
/*Function prototypes*/
void Gender(struct strPerson *arPeople[5]);
void FirstName(struct strPerson *arPeople[5]);
void Surname(struct strPerson *arPeople[5]);
void Name(struct strPerson *arPeople[5]);
void YearOfBirth(struct strPerson *arPeople[5]);
void WholeDOB(struct strPerson *arPeople[5]);
void DOB(struct strPerson *arPeople[5]);
void YearOfStart(struct strPerson *arPeople[5]);
int main(void)
{
int nChoice = 1;
struct strPerson arPeople[5];
/*populating structure within array "arPeople"*/
strcpy(arPeople[0].arcFirstName, "David");
strcpy(arPeople[0].arcLastName, "Hodgkiss");
arPeople[0].cSex = 'M';
arPeople[0].strDOB.nDay = 13;
arPeople[0].strDOB.nMnth = 5;
arPeople[0].strDOB.nYear = 1964;
arPeople[0].strStartDate.nDay = 1;
arPeople[0].strStartDate.nMnth = 9;
arPeople[0].strStartDate.nYear = 2001;
while (nChoice != 5)
{
printf("\nPlease enter number relating to search option required...\n");
printf("1 Search by Gender\n");
printf("2 Search by Name\n");
printf("3 Search by Date of Birth\n");
printf("4 Search by Start Date\n");
printf("5 Exit\n\n");
printf("Please enter your choice : ");
scanf("%d", &nChoice);
printf("\n");
switch (nChoice)
{
case 1: Gender(arPeople);
break;
case 2: Name(arPeople);
break;
case 3: DOB(arPeople);
break;
case 4: YearOfStart(arPeople);
break;
case 5: break;
default: printf("Invalid input, please try again : \n\n");
}
}
system("pause");
return 0;
}
void Gender(struct strPerson *arPeople[5])
{
char cSexMF = 'M';
printf("Please enter 'M' to search for male members of staff or 'F' to search for female memebers of staff : \n\n");
scanf("%c \n", &cSexMF);
printf(".... %c ... %c ....\n", cSexMF, arPeople[0].cSex);
}
So this is obviously just to search by Gender at the moment... my questions are:
How do I call the function in main in the menu system? At the moment I keep getting the error "warning C4047: 'function' : 'strPerson **' differs in levels of indirection from 'strPerson [5]'". I literally have no idea what this means.
Assuming I've passed it to the function correctly, how do I then print anything from the array? Where I have tried above to print
arPeople[0].cSexit says "expression must have a struct or union type". I don't understand this because I thought I'd passed it as a structure and so it would know what I am referencing.
I'd very much appreciate some help with this, I've looked everywhere I can think of, searched everything I can think of and I still can't make it work. I've been sitting here for days trying to work this out and am getting to the point where I don't even care if I fail my class.
void YearOfStart(struct strPerson *arPeople[5])<-- Remove the asterisk. Once you do this, everything will be alrightT a[]andT a[N]are interpreted asT *a). The only time an array is copied into a function parameter is if it's being passed as a member of a struct instance.