This is an assignment, but I've been making an honest jab at it.
We're supposed to make a contact list program, in which we use an array of structures to hold up to three contacts. The contacts are required to be held in a structure with the fields phone and ID.
-ID has to be a union type with fields of SSN and StudentID
-SSN has to be a structure with fields of prefix, number, and suffix
-phone has to be a structure with fields of area, prefix, suffix
We have to ask the user for the input, of course. If requested, the contact information is printed for each contact.
So here's some of my code of my current try (which is still mostly uncompleted):
Nested Structures
Code:
typedef struct {
int prefix,
number,
suffix;
} social_secnum;
typedef union{
int SchoolID;
social_secnum SSN;
} identify;
typedef struct {
int area,
prefix,
suffix;
} phone_number;
typedef struct contact_struct {
identify ID;
phone_number phone;
} contacts[3];
Function to populate structures?
Code:
void newContact (contact_struct contacts[3]) {
char contact_menu, dash;
int i;
for (i = 0; i < 3 ; i++) {
printf ("I) Enter the Student ID of this person\nS) Enter the SSN of this person\n");
scanf ("%c" &contact_menu);
switch (contact_menu) {
case 'I':
case 'i':
printf ("900");
scanf ("%d" , &identify.SchoolID);
contact_struct contacts[i].ID = &identify.SchoolID;
break;
case 'S'
case 's':
printf ("SSN: " );
scanf ("%d%c%d%c%d" , &social_secnum.prefix, &hyphen, &social_secnum.number, &hyphen, &social_secnum.suffix);
contact_struct contacts[i].ID = &identify.SSN;
break;
default:
printf ("Invalid input. Please try again.");
i = 0;
break;
}
printf ("Phone #: ");
scanf ("%d%c%d%c%d" , &phone_number.area, &hyphen, &phone_number.prefix, &hyphen, &phone_number.suffix);
contact_struct contacts[i].phone = &phone_number
}
}
Function to print contact info?
Code:
void printContact () {
int i;
for (i = 1; i < 4; i++) {
printf ("Contact #%d\n" , i);
if (contacts[i] ID == &identify.SchoolID) {
printf ("900%d\n" , contacts[i-1].ID.SchoolID);
}
else if (contacts[i] ID == &identify.SSN) {
printf ("%d-%d-%d" , contacts[i-1].ID.SSN.prexix, contacts[i-1].ID.SSN.number, contacts[i-1].ID.SSN.suffix);
}
printf ("(%d)-%d-%d" , contacts [i-1].phone);
}
I know it's not commented, which is really really bad practice... but I wanted to get things figured out before I started commenting... because I'm not quite sure what I'm doing right now. The code is still for the most part unfinished, so yeah. I understand structures, but we've been set out on our own to figure out nested structures, and arrays of structures.
When I compile, these are the errors I get
Code:
contactlist.c:26: error: expected ‘)’ before ‘contacts’
contactlist.c: In function ‘main’:
contactlist.c:39: warning: implicit declaration of function ‘newContact’
contactlist.c:39: error: expected expression before ‘contacts’
contactlist.c:42: warning: implicit declaration of function ‘printContact’
contactlist.c:57: error: expected ‘)’ before ‘contacts’
contactlist.c: In function ‘printContact’:
contactlist.c:91: error: expected expression before ‘contacts’
contactlist.c:92: error: expected expression before ‘contacts’
contactlist.c:94: error: expected expression before ‘contacts’
contactlist.c:95: error: expected expression before ‘contacts’
contactlist.c:95: warning: too few arguments for format
contactlist.c:97: error: expected expression before ‘contacts’
contactlist.c:97: warning: too few arguments for format
contactlist.c:98: error: expected declaration or statement at end of input
contactlist.c: In function ‘main’:
contactlist.c:98: error: expected declaration or statement at end of input
I'm not expecting anyone to count the lines on the code provided here to find where each of the errors/warnings are at, but I have some questions based on the errors/warnings.
-When it's warning me about the implicit declaration of functions, this obviously has to do with the arguments of the function prototype. What am I supposed to pass into the function? Should I not pass in anything since there's no input from the user to put into the argument that goes into the function?
-In the newContact function, do I have things going the right directions, as far as placing values and storing them into each of the structures? With all of the nesting going on, I'm not sure how one is read from the other.
-In the printContact function, I'm racking my brain on how to print the correct info. We use a union for either SSN, or StudentID because the contact will be defined by one, but not the other... so how can I get it to "know" which one to print out (I tried using the if and else if statements to do this)?
-Also in the printContact function, how do I get it to print out the correct data from all the nested structures? Do I have to try and access each field from the contacts structure? It keeps telling me that I don't have enough arguments to print out the data.
Any comments/criticism is definitely welcome. I can't find any good examples of nested structures and/or printing from them, so I'm having a hard time with this program.