
Kat H.
asked 06/28/21Create a C++ program that uses an array of struct Contact values.
Create a program that uses an array of struct Contact values. Please include source code.
What to code
The Contact struct is defined as follows:
Contact has:
• name
• phoneNumber
A PhoneNumber has:
• areaCode -> 3 digit regional code
• prefix -> first 3 digits
• suffix -> last 4 digits
associated with a phone number in 3 separate fields.
You can model the PhoneNumber as a separate struct and use it inside the Contact struct. Or you can just put the phone info in as separate fields. But you need to have at least the Contact struct in your code.
Then have your program ask the user for all the information for a contact. Use getline() for the name (names might have spaces). Also ask if they want to quit (please put this in; I won't be typing 100 contacts in!).
When the user is done, print out the contact information. See below for example output.
Requirements
You need to have an array of 100 Contact structs. All string fields should be C strings
only; no string type!
-----------------------
EXAMPLE OUTPUT #1
-----------------------
== Contact 1
Enter Name: Helen Wecker
Enter Phone Number's Area Code: 234
Enter Phone Number's Prefix: 535
Enter Phone Number's Suffix: 2364
Enter email: [email protected]
Do you want to add another? ('y' or 'n') y
== Contact 2
Enter Name: Maria Hernandez
Enter Phone Number's Area Code: 325
Enter Phone Number's Prefix: 908
Enter Phone Number's Suffix: 2352
Enter email: [email protected]
Do you want to add another? ('y' or 'n') n
Printing Contacts
== Contact 1
Name: Helen Wecker
Phone Number: (234) 535 - 2364
Email: [email protected]
== Contact 2
Name: Maria Hernandez
Phone Number: (325) 908 - 2352
Email: [email protected]
1 Expert Answer

Patrick B. answered 06/30/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct _TPhoneNumber
{
int area_code;
int prefix;
int suffix;
} * TPhoneNumber;
#define PHONE_NUMBER_SIZE (sizeof(struct _TPhoneNumber))
#define NAME_LENGTH (50)
#define EMAIL_LENGTH (255)
typedef struct _TContact
{
char firstName[NAME_LENGTH];
char lastName[NAME_LENGTH];
struct _TPhoneNumber phoneNumber;
char email[EMAIL_LENGTH];
} * TContact;
#define CONTACT_SIZE (sizeof(struct _TContact))
//converts phone # of the form XXX-XXX-XXXX to struct _TPhoneNumber
PhoneNumber_Parse(char * phoneNum,TPhoneNumber phoneNumber)
{
phoneNumber->area_code = atoi(strtok(phoneNum,"-"));
phoneNumber->prefix = atoi(strtok(NULL,"-"));
phoneNumber->suffix = atoi(strtok(NULL,","));
}
//converts struct _TPhoneNumber to string buffer of the form XXX-XXX-XXXX
PhoneNumber_Serialize(char * phoneNum,TPhoneNumber phoneNumber)
{
sprintf(phoneNum,"%d-%d-%d",phoneNumber->area_code,phoneNumber->prefix,phoneNumber->suffix);
}
#define MAX_CONTACTS (100)
typedef struct _TContactList
{
struct _TContact contacts[MAX_CONTACTS];
int count;
} * TContactList;
#define CONTACT_LIST_SIZE (sizeof(struct _TContactList))
int Contacts_Read(TContactList Contacts, char * filename)
{
FILE * fptr;
fptr = fopen(filename,"r");
int iReturn=0;
char phoneNumStr[25];
if (fptr!=NULL)
{
fscanf(fptr,"%d",&Contacts->count);
for (int iLoop=0; iLoop<Contacts->count; iLoop++)
{
fscanf(fptr,"%s %s %s %s",&Contacts->contacts[iLoop].lastName,
&Contacts->contacts[iLoop].firstName,
phoneNumStr,
&Contacts->contacts[iLoop].email);
PhoneNumber_Parse(phoneNumStr,&Contacts->contacts[iLoop].phoneNumber);
}
fclose(fptr);
}
else
{
printf(" error opening and/or reading the input file \n");
iReturn=-1;
}
return(iReturn);
}
int Contacts_Write(TContactList Contacts, char * filename)
{
int iReturn=0;
char phoneNumStr[25];
FILE * fptr = fopen(filename,"w");
if (fptr!=NULL)
{
fprintf(fptr,"%d \n",Contacts->count);
for (int iLoop=0; iLoop<Contacts->count; iLoop++)
{
PhoneNumber_Serialize(phoneNumStr,&Contacts->contacts[iLoop].phoneNumber);
fprintf(fptr,"%s %s %s %s \n",Contacts->contacts[iLoop].lastName,
Contacts->contacts[iLoop].firstName,
phoneNumStr,
Contacts->contacts[iLoop].email);
}
fclose(fptr);
}
else
{
iReturn=-1;
printf(" ERROR OPENING OUTPUT FILE \n");
}
return(iReturn);
}
int Contacts_InsertAddNew(TContactList Contacts, TContact newContact)
{
memcpy(&Contacts->contacts[Contacts->count++],newContact,CONTACT_SIZE);
Contacts_Write(Contacts,"E:\\contacts.dat");
}
int Contacts_LinearSearchFind(TContactList Contacts, TContact targetContact)
{
int iReturn=-1;
for (int iLoop=0; iLoop<Contacts->count; iLoop++)
{
if (
( stricmp(targetContact->firstName,Contacts->contacts[iLoop].firstName) ==0 ) &&
( stricmp(targetContact->lastName ,Contacts->contacts[iLoop].lastName ) ==0)
)
{
iReturn=iLoop;
break;
}
}
return(iReturn);
}
int Contacts_RemoveDelete(TContactList Contacts, TContact targetContact)
{
int iReturn=0;
int iLinearSearchReturnResult = Contacts_LinearSearchFind(Contacts,targetContact);
if (iLinearSearchReturnResult>=0)
{
//moves the contact records 1 position to the left <--; target record overwritten
for (int iLoop=iLinearSearchReturnResult; iLoop<Contacts->count; iLoop++)
{
memcpy(&Contacts->contacts[iLoop],&Contacts->contacts[iLoop+1],CONTACT_SIZE);
}
//invalidates the last record
memset(&Contacts->contacts[Contacts->count-1],0,CONTACT_SIZE);
}
else
{
printf(" contact record to delete is not found in the list \n");
iReturn=-1;
}
return(iReturn);
}
main()
{
struct _TContactList Contacts;
Contacts_Read(&Contacts,"E:\\contacts.dat");
for (int iLoop=0; iLoop<Contacts.count; iLoop++)
{
char strPhoneNum[25];
memset(strPhoneNum,0,25);
PhoneNumber_Serialize(strPhoneNum,&Contacts.contacts[iLoop].phoneNumber);
printf(" Contact # %d : \n ---------------------------\n",(iLoop+1));
printf("Name: %s %s \n",Contacts.contacts[iLoop].firstName,Contacts.contacts[iLoop].lastName);
printf(" Phone Number: %s \n",strPhoneNum);
printf(" Email: %s \n\n",Contacts.contacts[iLoop].email);
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
Again, there is not enough room to post all of source code needed. There is 2000 character limit. You can email me by clicking on my picture in my profile. There will be a link to send me an email06/30/21