// Header file first
#ifndef _ADDRESSBOOK
#define _ADDRESSBOOK
const int MAXADDRESS = 25;
struct PERSON
{
char fName[25];
char lName[25];
char Address[100];
};
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();
#endif
// addressBook.cpp
#include "addressBook.h"
#include <cstring> // <--- RX: Needed by stricmp and other string functions
#include <iostream> // <--- RX: Needed cout and other I/O streams
using namespace std;
PERSON people[MAXADDRESS];
int head = 0;
int tail = -1;
bool addPerson(const PERSON &p)
{
if(head < MAXADDRESS)
{
people[head] = p;
head++;
if(tail == -1)
tail++;
return true;
}
return false;
}
bool getPerson(PERSON &p)
{
if(tail >=0)
{
if(tail >= MAXADDRESS)
tail = 0;
p = people[tail];
tail++;
return true;
}
return false;
}
bool 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 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()
{
for(int i = 0; i < head; i++)
{
cout << people[i].fName << "\t" << people[i].lName << "\t" << people[i].Address << endl;
}
}
// finally main.cpp
#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()
{
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 = 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 = 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 = 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 = 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;
}
#include <cstdlib> // <--- RX: For the system() function
void waitKey()
{
// <--- RX: On Windows just use system("pause");
//cout << "Press a key to continue " << endl;
//while(!kbhit())
// ;
//getch();
//fflush(stdin);
system("pause"); // This only works on Windows!
}
is code that works fine ^^^^
now to move it to classes i tried to do this:
// 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);
}// Header file first
#ifndef _ADDRESSBOOK
#define _ADDRESSBOOK
const int MAXADDRESS = 25;
class PERSON
{
private:
char fName[25];
char lName[25];
char <strong class="highlight">Address</strong>[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 <strong class="highlight">with</strong> 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 <strong class="highlight">Address</strong>[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 <strong class="highlight">Address</strong> " << endl;
cin >> p.Address;
status = access.addPerson(p);
if(status == false)
cout << "Sorry There is no more room in the <strong class="highlight">address</strong> <strong class="highlight">book</strong> " << 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 <strong class="highlight">address</strong> <strong class="highlight">book</strong> 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 <strong class="highlight">address</strong> <strong class="highlight">book</strong> " << 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 <strong class="highlight">address</strong> 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);
}
but it is NOT working. :(
what have i done wrong, this is so discouraging. i really really hate c++ now. lol
:(
Edited by WingedPanther, 18 December 2008 - 06:47 PM.


Sign In
Create Account


Back to top









