Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Addressbook problem that I can't seem to locate (constructors)

$
0
0
OK, I am doing a lab for school, and it looks like, (at least to me), that I have what I need. I have an addressbook program that is supposed to use a class, and constructors. the requirements are:

Quote

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


So I have the second part good to go, and I'm pretty sure I have the first and third parts done correctly, but neither will print out when I run the program. This is homework, and I'm not looking for code or to cheat, I actually do want to understand and learn this, but I was wondering if someone could look over this real quick and see if they can find why neither the first or third requirements are printing out, and possibly point me in right direction on getting the problem solved. I have 3 files...

My header file:

#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:

 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();
 PERSON part1;
 addressBook();
	

 addressBook ad();
 addressBook myBook();
 addressBook newBook();
 addressBook newerbook();

 addressBook( char *fName, char *lName, char *Address );

 addressBook( PERSON p );

 addressBook( PERSON *p, int size );
	 
	
};
#endif


MY addressBook.cpp file:

#include <iostream>
#include "addressBook.h"
using namespace std;
addressBook::addressBook()
{
 head = 0;
 tail = -1;
}

addressBook::addressBook( PERSON p )
{
   addPerson( p );
};

addressBook::addressBook( char *fName, char *lName, char *Address  )
{
	PERSON p;
   strcpy_s( p.fName, fName );
   strcpy_s( p.lName, lName );
   strcpy_s( p.Address, Address );
   addPerson( p );
};

addressBook::addressBook( PERSON *p, int size )
	{
		for ( int i = 0; i < size; i++ )
   {
       PERSON p[] = { 
{"BOB", "Robertson", "1313 Mockingbird ln"},
{"Joe", "Smith", "1023 Anywhere"},
{"Jane", "Doe", "555 self place"}
};
	  addPerson(p[i]);
   }

};

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 my main.cpp:

#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()
{
	PERSON p;
	addressBook ad;
	addressBook myBook;


	PERSON me = {"Someone", "Somebody", "123 house"};


	myBook.addPerson(me);


	addressBook newbook("TEST", "TEST2", "1234"); 

	addressBook newerbook;

	
	
	
	

int selection;



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 :
				newerbook.printBook();
				newbook.printBook();
				ad.printBook();
				myBook.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);

};



If someone could let me know what I'm doing wrong, I would really appreciate it, thanks.

Just a quick note, I got a lot of help on this from iHutch105 on another forum, and wanted to give props for all the work he did helping me understand this.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>