1.) Create a project called addressBook
2.) Add a header file and cpp file for your project
3.) All of your definitions should go in the header file.
4.) In the header file create the definition for your structure. Call it PERSON.
5.) Your structure should have fields for first name, last name, address, and optionally a phone number.
6.) Inside the cpp file you will create the functionality for your address book
7.) Inside the cpp file declare a global array of 10 PERSONS to hold all of the records in your address book call it people. Use a const called MAXPEOPLE to set the size of the array. Put the const in the header file.
8.) You are probably going to want to declare an integer variable to keep track of where you are at in the array.
9.) Create functions addPerson, getPerson
10.) These functions should take as arguments a reference to a PERSON structure.
11.) The addPerson method should copy the structure passed to it to the end of the array
12.) the getPerson should start at array element 0 and with each successive call return the next person in the array.
13.) Create overloaded findPerson functions. One function should take only the persons last name
14.) The other function should take both the persons last and first names.
15.) All code for the functions should be in the cpp file
16.) From main write functionality that will test your address book code
Ok, so you should have three files
main.cpp - holds you main function
addressBook.cpp - holds the address book functions that are required for the assignment
addressBook.h - which holds the definitions for addressBook.cpp
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include "addressBook.h"
int maincounter = 0;
int getcount = 0;
using namespace std;
void addPerson(struct person &add);
void getPerson(struct person &get);
void printStruct(struct person &print);
void findPerson(struct person &find);
void findPerson(struct person &findlast, char &last, char &first);
person bookArray[MAXPEOPLE];
int main()
{
char *last = new char[20];
char *first = new char[20];
char choice;
person add;
person getStruct;
person findStruct1;
person findStruct2;
cout << "Address Book" << endl << endl;
while (true)
{
cout << endl << " 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: " << endl;
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 << endl << "Getting the next person in the address book: " << endl << endl;
getPerson(getStruct);
printStruct(getStruct);
break;
case '3':
cout << "Please enter the last name to search for: " << endl;
cin >> last;
findPerson(findStruct1, 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(findStruct2, 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, char &last)
{
for (int i=0; i<=MAXPEOPLE; i++)
{
if (find == find.first)
{
}
else
{
cout << "Sorry, that entry was not found " << endl;
}
}
}
void findPerson (struct person &findlast, char &last, char &first)
{
for (int i=0; i<=MAXPEOPLE; i++)
{
if (findlast.last == last || findfirst.first == first)
{
printStruct(findlast)
}
else
{
cout << "Sorry, that entry was not found " << endl;
}
}
}
void printStruct(struct person &print)
{
cout << print.first << " " << print.last << endl;
cout << print.address << endl;
}
| |
Addressbook.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#pragma once
#ifndef ADDRESSBOOK
#define ADDRESSBOOK
const int MAXPEOPLE = 10;
struct person
{
char first[20];
char last[20];
char address[50];
};
class addressBook
{
private:
person p[MAXPEOPLE];
int count;
public:
addressBook();
addressBook(cString fName, cString lName, cString phone) {
self.addPerson(fName, lName, phone);
}
bool addPerson(const person p);
bool addPerson(const char *fName,const char *lName, const char *phone);
bool getPerson(person &p);
bool getPerson(char *fName, char *lName, char *Phone);
};
#endif
| |
addressbook.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <iostream>
#include "addressBook.h"
using namespace std;
addressBook::addressBook()
: head(0), tail(-1)
{
}
addressBook::addressBook(const person &p)
: head(0), tail(-1)
{
addPerson(p);
}
addressBook::addressBook(const person p[], int size)
: head(0), tail(-1)
{
for(int i = 0; i < size; i++)
addPerson(p[i]);
}
addressBook::addressBook(char *fName, char *lName, char *address)
: head(0), tail(-1)
{
person tmp;
strcpy(tmp.fName, fName);
strcpy(tmp.lName,lName);
strcpy(tmp.Address, address);
addPerson(tmp);
}
bool addressBook::addPerson(const person &p)
{
if(head < MAXADDRESS)
{
people[head] = p;
head++;
if(tail == -1)
tail++;
return true;
}
return false;
}
bool addressBook::getPerson(person &p)
{
if(tail >=0)
{
if(tail >= MAXADDRESS)
tail = 0;
p = people[tail];
tail++;
return true;
}
return false;
}
bool addressBook::findPerson(char *lastName, person &p)
{
for(int i = 0; i < head; i++)
{
if(!stricmp(people[i].lName, lastName))
{
p = people[i];
return true;
}
}
return false;
}
bool addressBook::findPerson(char *lastName, char *firstName, person &p)
{
for(int i = 0; i < head; i++)
{
if(!stricmp(people[i].lName, lastName) && !stricmp(people[i].fName, firstName))
{
p = people[i];
return true;
}
}
return false;
}
void addressBook::printBook()
{
for(int i = 0; i < head; i++)
{
cout << people[i].fName << "\t" << people[i].lName << "\t" << people[i].Address << endl;
}
}
void addressBook::sort()
{
person temp;
for(int i = 0; i < head; i++)
{
for (int j=0; j < head; j++)
{
if (stricmp(people[j+1].lName, people[j].lName) < 0)
{
temp = people[j];
people[j] = people[j+1];
people[j+1] = temp;
}
}
}
}
ostream& operator<<(ostream& output, const Point& p) {
output << "(" << p.x << ", " << p.y <<")";
return output;
}
bool& operator+= (const person p);
{
return addPerson(const p);
}
person& operator() (int i)
{
return people[i];
}
| |