Write a program that keeps track of a list of contacts, consisting of names and
ID: 3575373 • Letter: W
Question
Write a program that keeps track of a list of contacts, consisting of names and telephone numbers. The program should use a structure (call it ContactInfo) for each contact, storing information in three fields:
Last Name
First Name
Telephone Number
The program should use an array to store these structures. It should let the user add contacts
(up to maxNum), change the contents of any contact, look up t
elephone numbers by first name
(showing all numbers and last names of contacts with this first name), and display all of the datastored in the array. The program should have a menu driven interface. When the data for a new contact is entered, the user must enter data for all the fields. Telephone numbers must be in the usual North American format, such as 212- 650-7000. First and last names should be entered separately. To do this program effectively, you MUST do this in stages.
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
#define maxNum 10
//Structure Definition Contact
struct ContactInfo
{
string firstname;
string lastname;
string telephoneNumber;
};
//contact info Function Prototypes
void displayMenu();
void printContact(ContactInfo);
void updateContact(ContactInfo[],string);
void findContact(ContactInfo[],string);
ContactInfo addContact();
int main() {
// Declare an array of contact info objects
struct ContactInfo contact[5];
int count=0, choice, index;
string firstName;
do{
displayMenu();
cin >> choice;
cin.ignore(); // remove " " from input stream
switch(choice) {
/* case 1, 2, 3 ,4*/
case 1: cout<<" Add new contact details";
contact[count]= addContact();
count++;
break;
case 2: cout<<" update Contact";
cout<<" Enter firstname of the person you want to update telephone number";
cin>>firstName;
updateContact(contact,firstName);
break;
case 3: cout<<" Find Contact";
cout<<" Enter firstname of the person you want to find telephone number";
cin>>firstName;
findContact(contact,firstName);
break;
case 4: cout<<" List of contacts";
for(int i=0;i<5;i++)
printContact(contact[i]);
break;
case 0:
cout << "Exiting Program ";
break;
default:
cout << "Invalid Option ";
}
}while(choice!=0);
cout << "Good Bye ";
return (0);
}
//Employee Function Definitions
void displayMenu() {
cout<<" ---------------------------- "
<< "| Contact Diary | "
<< "|----------------------------| "
<< "| 1.Add New Contact | "
<< "| 2.Update Contact | "
<< "| 3.Find Contact | "
<< "| 4.Print Contacts | "
<< "| 0.Exit | "
<< " ---------------------------- "
<< " Enter Choice: ";
}
void printContact(ContactInfo temp)
{
cout<<" "<<temp.firstname <<" "<<temp.lastname <<" "<<temp.telephoneNumber;
}
void findContact(ContactInfo contact[5],string firstname)
{
int i;
for(i=0;i<5;i++)
{
if(contact[i].firstname == firstname)
{
cout<<contact[i].firstname<<endl;
cout<<contact[i].lastname<<endl;
cout<<contact[i].telephoneNumber;
}
}
}
void updateContact(ContactInfo contact[5],string firstname)
{
int i;
for(i=0;i<5;i++)
{
if(contact[i].firstname == firstname)
{
cout<<"Enter new first name ";
cin>>contact[i].firstname;
cout<<"Enter new last name ";
cin>>contact[i].lastname;
cout<<"Enter new telephone number ";
cin>>contact[i].telephoneNumber;
}
}
}
ContactInfo addContact()
{
struct ContactInfo contact;
cout<<" Enter first name";
cin>>contact.firstname;
cout<<" Enter last name";
cin>>contact.lastname;
cout<<" Enter Telephone Number";
cin>>contact.telephoneNumber;
return contact;
}
output:
0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.