Language: C++ 1. Write a program that will manage a simple address book. The pro
ID: 3676789 • Letter: L
Question
Language: C++
1. Write a program that will manage a simple address book. The program should allow you to view and search existing entires as well as add new entries. The data should be stored in a file structured so that a person's first name, last name, and phone number are stored on one line separated by commas and the next line has their address. (Fully documented program in separated .cpp and .h files)
Additional requirements:
The program should allow the user to make repeated requests until they choose to exit
The program should show multiple matches if present
The program should have an option to print out a list that just includes Name and Phone Number
The program should use at least two struct types and one enumerated type
Explanation / Answer
//Header file header.h
const int MaxPeople = 10;
struct person
{
char *first;
char *last;
char *address;
char *number;
};
// c++ file
#include <iostream>
#include "header.h"
int maincounter = 0;
int getcount = 0;
using namespace std;
void addPerson(struct person &add);
void getPerson(struct person &get);
void findPerson (struct person &find);
void findPerson (struct person &findlast, struct person &findfirst);
person bookArray[MaxPeople]; //This is the main "address book" array
int main()
{
char *last = new char[20];
char *first = new char[20];
char choice;
person add;
person getStruct;
cout << "Address Book" << endl<< endl;
while (true)
{
cout << " Enter 1 to add to the address book. " <<endl;
cout << " Enter 2 to get a person. " << endl;
cout << " Enter 3 to find a person by last name only." << endl;
cout << " Enter 4 to find a person by last name and first. " << endl;
cout << " Enter any other key to quit. " << endl;
cout << endl << "Please make a selection: ";
cin >> choice;
switch(choice)
{
case '1':
cout << "Enter First Name" << endl;
cin >> add.first;
cout << "Enter Last Name" << endl;
cin >> add.last;
cout << "Enter Address" << endl;
cin.getline(add.address, 40);
addPerson(add);
break;
case '2':
cout << "Getting the next person in the address book: " << endl << endl;
getStruct = getPerson(getStruct)
cout << getStruct << endl;
break;
case '3':
cout << "Please enter the last name to search for: " << endl;
cin >> last;
findPerson(last);
break;
case '4':
cout << "Please enter the last name and then the first name to search for: " << endl;
cout << "Last name: ";
cin >> last;
cout << "First name: ";
cin >> first;
findPerson(last, first);
break;
default:
return 0;
break;
}
}
return 0;
}
void addPerson(struct person &add)
{
bookArray[maincounter]=add;
maincounter++;
}
void getPerson(struct person &get)
{
if (getcount > maincounter)
{
getcount = 0;
get = bookArray[getcount];
getcount++;
}
else
{
get = bookArray[getcount];
getcount++;
}
}
void findPerson (struct person &find)
{
for (int i=0; i<=MaxPeople; i++)
{
if (find == person.last)
{
// Display all the data from the array location that holds the structure with the matching value.
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.