Okay, well, I got my Address Book project done on time :) My current project is to turn it into a class. I have been working on this for a while and I keep getting set back to square one. My biggest problem is with writing some sort of user interface while dealing with private variables. I have read some posts by the Queen of Daniweb, which were helpful, but I can't get to her tutorial.... anyway, if someone could just explain how a person interfaces with private variables, I would appreciate it. Just something to get me started. Anyway, here's the code so far:
// Header file first
#ifndef _ADDRESSBOOK
#define _ADDRESSBOOK
const int MAXADDRESS = 25;
class PERSON
{
private:
char fName[25];
char lName[25];
char Address[100];
char people[MAXADDRESS];
public:
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(PERSON &p);
};
#endif
//Now the cpp with all the functions...
#include <iostream>
using namespace std;
#include "definitionsAddressBook.h"
using namespace std;
int head = 0;
int tail = -1;
bool PERSON::addPerson(const PERSON &p)
{
if(head < MAXADDRESS)
{
people[head] = p;
head++;
if(tail == -1)
tail++;
return true;
}
return false;
}
bool PERSON::getPerson(PERSON &p)
{
if(tail >=0)
{
if(tail >= MAXADDRESS)
tail = 0;
p = people[tail];
tail++;
return true;
}
return false;
}
bool PERSON::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 PERSON::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 printBook(PERSON &p)
{
for(int i = 0; i < MAXADDRESS; i++)
{
cout << people[i].fName << "\t" << people[i].lName << "\t" << people[i].Address << endl;
}
}
// Now the main cpp
#include <iostream>
using namespace std;
#include "definitionsAddressBook.h"
#include "conio.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()
{
int selection;
PERSON p;
PERSON access;
bool status;
char fName[50];
char lName[50];
char Address[100];
char *firstName;
char *lastName;
char *address2;
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 = access.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 = access.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 = access.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 = access.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 :
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);
}
Anyway, I can't have any cin or cout in the functions - beileve me, I already tried. If anybody has a suggestion, I would really appreciate it.