Ok, I just started my c++ level two class, after a six year break from level one. I'm not allowed to retake level one unfortunately, so I'm struggling a bit in level two.
My latest assignment has me having to add constructors to my current address book program. I have been trying to do this for almost a week, and for the life of me I just cannot figure out how to get them to work at all.
I'm not looking for someone to answer or solve this for me, but I really need help, and I am desperate. Can anyone please help me understand what I need to do and how to do it.
----------------------------
Here's my header file:
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
|
#ifndef _ADDRESSBOOK
#define _ADDRESSBOOK
const int MAXADDRESS = 25;
struct PERSON
{
char fName[25];
char lName[25];
char Address[100];
};
class addressBook
{
private:
PERSON people[MAXADDRESS];
int head;
int tail;
public:
addressBook();
bool addPerson(const PERSON &p);
bool getPerson(PERSON &p);
bool findPerson(char *lastName, PERSON &p);
bool findPerson(char *lastName, char *firstName, PERSON &p);
void printBook();
static addressBook *Instance();
};
#endif
| |
------------------------------------------------
here is my addressbook .cpp file:
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
|
#include <iostream>
#include "addressBook.h"
using namespace std;
addressBook::addressBook()
{
head = 0;
tail = -1;
}
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 >= head)
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;
}
}
| |
-------------------------------
and here is my 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
|
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include "addressBook.h"
using namespace std;
int printMenu();
void waitKey();
const int ADDPERSON = 1;
const int GETPERSON = 2;
const int FINDLAST = 3;
const int FINDBOTH = 4;
const int PRINT = 5;
const int EXIT = 0;
int main()
{
addressBook ad;
int selection;
PERSON p;
bool status;
char lName[50];
char fName[50];
selection = printMenu();
while(selection != EXIT )
{
switch(selection)
{
case ADDPERSON :
cout << "Enter First Name " << endl;
cin >> p.fName;
cout << "Enter last Name " << endl;
cin >> p.lName;
cout << "Enter Address " << endl;
cin >> p.Address;
status = ad.addPerson(p);
if(status == false)
cout << "Sorry There is no more room in the address book " << endl;
else
cout << "Thanks for your Entry " << endl;
waitKey();
break;
case GETPERSON :
status = ad.getPerson(p);
if(status)
cout << p.fName << "\t" << p.lName << " " << p.Address << endl;
else
cout << "Sorry The address book is empty " << endl;
waitKey();
break;
case FINDLAST :
cout << "Enter a last name " << endl;
cin >> lName;
status = ad.findPerson(lName,p);
if(status)
cout << p.fName << "\t" << p.lName << " " << p.Address << endl;
else
cout << "Sorry, Name not found " << endl;
waitKey();
break;
case FINDBOTH :
cout << "Enter last name " << endl;
cin >> lName;
cout << "Enter first name " << endl;
cin >> fName;
status = ad.findPerson(lName, fName,p);
if(status)
cout << p.fName << "\t" << p.lName << " " << p.Address << endl;
else
cout << "Sorry, Name not found " << endl;
waitKey();
break;
case PRINT :
ad.printBook();
waitKey();
break;
case EXIT :
cout << "Thanks for using the address book " << endl;
exit(0);
}
selection = printMenu();
}
}
int printMenu()
{
int selection;
system("CLS");
cout << "1. Add A Person" << endl;
cout << "2. Get A Person " << endl;
cout << "3. Find A person By Last Name " << endl;
cout << "4. Find A person By First and Last Name " << endl;
cout << "5. Print the address book" << endl;
cout << "0. Exit this program " << endl;
cin >> selection;
return selection;
}
void waitKey()
{
cout << "Press a key to continue " << endl;
while(!kbhit())
;
getch();
fflush(stdin);
}
| |
-------------------------------------------------
Finally, here are my assignment requirements:
For the addressBook class you created in lab 2 create overloaded constructors that will accomplish the following:
Initialize a single person passing the informaition using three cStrings
Initialize a single person passing the information using a PERSON struct;
Initialize several people passing an array of PERSON structs and an int that holds the count
Make sure that your constructors do not allow you to add too many people to the address book, and make sure you set the count appropriately
Example use:
Constructor 1
addressBook ab("Bob", "Roberts","1313 Mockingbird ln.");
Constructor 2
PERSON p = {"Bob", "Roberts", "1313 Mockingbird ln"};
addressBook ab(p);
Constructor 3
PERSON p[] = {
{"Bob", "Roberts", "1313 Mockingbird ln"},
{"Joe", "Smith, "1023 Anywhere"},
{"Jane", "Doe", "555 self place"}
};
addressBook ab(p, 3);
Thank you in advance for anyone who has the time to help me with this.