Evening to everyone. I have a new project that is rather lengthy, but I am almost done with it. Sorry about the long read, but I was hoping to answer any questions that you may have upfront by posting all that I know about this project. Thanks

Here's the assignment:

Write a program that provides a way for you to store and retrieve telephone numbers. Design a user interface (for those that want an adventure, try using the GUI tools in Visual C++, or just make a usable interface that runs at the command prompt) that provides the following operations:

Add: Add a person’s name and phone number to the phone book.
Delete: Deletes a given person’s name and phone number from the phone book, given only the name.
Find: Locate a person’s phone number, given only the person’s name.

Change: Changes a person’s phone number, given the person’s name and new phone number.
Quit: Quits the application, after first saving the phone book in a text file.
Proceed as follows:

1. Design and implement the class Person, which represents the name and phone number of a person. You will store instances of this class in the phone book.
2. Design and implement the class PhoneBook, which represents the phone book. The class should contain a binary search tree as a data field. This tree contains the people in the book.
3. Add methods that use a text file to save and restore the tree. (Think about the traversal methods we discussed in class.)
4. Design and implement the class Menu, which provides the program’s user interface.
The program should read data from a text file when it begins and save data into a text file when the user quits the program.

My errors that I am getting:
Warning 1 warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\person.cpp 7
Warning 2 warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\person.cpp 9
Warning 3 warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\person.cpp 10
Warning 4 warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\person.cpp 17
Warning 5 warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\person.cpp 18
Warning 6 warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\person.cpp 19
Error 7 error C2360: initialization of 'pers' is skipped by 'case' label c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\menu.cpp 67
Error 8 error C2361: initialization of 'pers' is skipped by 'default' label c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\menu.cpp 72
Warning 9 warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\phonebook.cpp 205
Warning 10 warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\phonebook.cpp 207
Warning 11 warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\documents and settings\cs306\desktop\mcn312lab3\homework project 3\phonebook.cpp 208

#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
using namespace std;

class person
{
public:	
	person(); // default constructor
	person(string n); 
	person(string n, string pn); 
	friend bool operator< (person, person);
	friend bool operator> (person, person);
	friend bool operator==(person, person);
	person operator=(person);

	string fname;
	string lname;
	string phone;
}; // end Person
#endif

#include <iostream>
#include <cstdlib>
#include "person.h"
using namespace std;


class PhoneBook
{
private:
	struct tree_node
	{
		tree_node* left;
		tree_node* right;
		person data;
		void printnode();
	};
	tree_node* root;

public:
	PhoneBook()
	{
		root = NULL;
	}
	bool isEmpty() const { return root==NULL; }
	void load(const string& fname);
	void store(const string& fname);
	void find(person d);
	void print_inorder();
	void clear();
	void inorder(tree_node*);
	void insert(person);
	void remove(person);
	void edit(person d);

};

#include "person.h"
#include <string>
#include <cstring>

person::person(string n, string ph){
	char* cstr = new char[n.size()+1];
	strcpy(cstr,n.c_str());

	fname.assign(strtok(cstr," "));
	lname.assign(strtok(NULL," "));
	phone = ph;
}

person::person(string n)
{
	char* cstr = new char[n.size()+1];
	strcpy(cstr,n.c_str());
	fname.assign(strtok(cstr," "));
	lname.assign(strtok(NULL," "));
}

person::person()
{
	// Person is empty here
}

bool operator< (person p1, person p2){
	if(p1.lname == p2.lname)
		return p1.fname < p2.fname;
	else
		return p1.lname < p2.lname;
} 

bool operator> (person p1,person p2){
	if(p1.lname == p2.lname)
		return p1.fname > p2.fname;
	else
	return p1.lname > p2.lname;
} 

bool operator==(person p1,person p2){
	if( p1.fname == p2.fname && p1.lname == p2.lname && p1.phone == p2.phone )
		return true;
	else
		return false;
}  

person person::operator=(person p1){
	fname = p1.fname;
	lname = p1.lname;
	phone = p1.phone;
	return *this;
} 

#include "PhoneBook.h"
#include "person.h"
#include <fstream>

void PhoneBook::insert(person d)
{
	tree_node* t = new tree_node;
	tree_node* parent;
	t->data = d;
	t->left = NULL;
	t->right = NULL;
	parent = NULL;
	if(isEmpty()) root = t;
	else	{
		tree_node* curr;
		curr = root;
		while(curr){
			parent = curr;
			if(t->data > curr->data) 
				curr = curr->right; 
			else 
				curr = curr->left; 
		}
		if(t->data < parent->data) 
			parent->left = t;
		else
			parent->right = t;
	}
} 

void PhoneBook::remove(person d){
	bool found = false;
	if(isEmpty())	{
		cout<<" Tree has no nodes, empty tree. "<<endl;
		return;
	}
	tree_node* curr;
	tree_node* parent;
	curr = root;

	while(curr != NULL)	{
		if(curr->data.fname == d.fname && curr->data.lname == d.lname )	{
			found = true;
			break;
		}
		else	{
			parent = curr;
			if(d > curr->data) 
				curr = curr->right;
			else curr = curr->left;
		}
	}
	if(!found){
		cout<<" Cannot find any data! "<<endl;
		return;
	}
	if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL&& curr->right == NULL)){
		if(curr->left == NULL && curr->right != NULL) { 
			if(curr == root){ 
				root = curr->right;
				delete curr;
				return;
			}
			if(parent->left == curr)	{
				parent->left = curr->right;
				delete curr;
			}
			else {
				parent->right = curr->right;
				delete curr;
			}
		}
		else {
			if(curr == root){ 
				root = curr->left;
				delete curr;
				return;
			}
			if(parent->left == curr){
				parent->left = curr->left;
				delete curr;
			}
			else{
				parent->right = curr->left;
				delete curr;
			}
		}
		return;
	}

	if( curr->left == NULL && curr->right == NULL){
		if(curr == root){
			root = NULL;
			return;
		} 
		if(parent->left == curr) 
			parent->left = NULL;
		else 
			parent->right = NULL;
		delete curr;
		return;
	}
	if (curr->left != NULL && curr->right != NULL){
		tree_node* chkr;
		chkr = curr->right;
		if((chkr->left == NULL) && (chkr->right == NULL))	{
			curr = chkr;
			delete chkr;
			curr->right = NULL;
		}
		else {

			if((curr->right)->left != NULL){
				tree_node* lcurr;
				tree_node* lcurrp;
				lcurrp = curr->right;
				lcurr = (curr->right)->left;
				while(lcurr->left != NULL){
					lcurrp = lcurr;
					lcurr = lcurr->left;
				} 			
				curr->data = lcurr->data;
				delete lcurr;
				lcurrp->left = NULL;
			} 
			else	{
				tree_node* tmp;
				tmp = curr->right;
				curr->data = tmp->data;
				curr->right = tmp->right;
				delete tmp;
			} 
		} 
		return;
	} 

} 
void PhoneBook::print_inorder()
{
	inorder(root);  
}

void PhoneBook::inorder(tree_node* p)
{ 
	if(p != NULL)
	{
		if(p->left) inorder(p->left);
		p->printnode();
		if(p->right) inorder(p->right);
	}
	else 
		return; 
}

void PhoneBook::find(person d){
	
	bool found = false;
	if(isEmpty()) {
		cout<<" Tree has no nodes, empty tree. "<<endl;
		return;
	}

	tree_node* curr;
	tree_node* parent;
	curr = root;

	while(curr != NULL)	{
		if(curr->data.fname == d.fname && curr->data.lname == d.lname )	{
			found = true;
			curr->printnode();
			break;
		}
		else	{
			parent = curr;
		
			if(d > curr->data) 
				curr = curr->right;
			else curr = curr->left;
		}
	}
	if(!found)
	{
		cout<<" Cannot find any data! "<<endl;
		return;
	}
} 

void PhoneBook::clear(){
	root = NULL; 
}

void PhoneBook::tree_node::printnode(){
	cout<<data.fname + " " + data.lname + " - " + data.phone + "\n";
}
void PhoneBook::load(const string& fname) {
	ifstream in( fname.c_str() );
	string n,p;
	
	if ( in.is_open() ) {
		while (!in.eof()){ 
			getline(in,n); 
			if(n.empty()) 
				break;
			char* cstr = new char[n.size()+1]; 
			strcpy(cstr,n.c_str()); 
			n.clear(); 
			n.assign(strtok(cstr,"-")); 
			p.assign(strtok(NULL," \n")); 
			person pers(n,p); 
			insert(pers); 
		}
		in.close(); 
		if(!in.is_open())
			cout << "Finished Loading File!\n";
		else
			cout << "Error Closing File!\n";
	}
	else
		cout << "Error opening file!\n";
}



void PhoneBook::store(const string& fname) {
  ofstream out(fname.c_str());
  if(out.is_open())
	  cout << "Opening for writing...\n";
  else
	  cout << "Error opening file!\n";

	  streambuf* sbuf = cout.rdbuf();
	  cout.rdbuf(out.rdbuf());
	  print_inorder();
	  
	  cout.rdbuf(sbuf);

	  cout << "Finished writing!\n";
	  out.close();
	  if(!out.is_open()){
		  cout << "File Closed!\n";
		  return;
	  }
	  else
		  cout<<"Error Closing file!";
}
void PhoneBook::edit(person d)
{
	bool found = false;
	if(isEmpty()) {
		cout<<" This Tree is empty! "<<endl;
		return;
	}

	tree_node* curr;
	tree_node* parent;
	curr = root;

	while(curr != NULL)	{
		if(curr->data.fname == d.fname && curr->data.lname == d.lname )	
		{
			found = true;
			curr->data.phone = d.phone;

			curr->printnode();
			break;
		}
		else	{
			parent = curr;
		
			if(d > curr->data) 
				curr = curr->right;
			else curr = curr->left;
		}
	}
	if(!found)
	{
		cout<<" Data not found! "<<endl;
		return;
	}
}


#include "person.h"
#include "PhoneBook.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
char get_input();


int main() {

	PhoneBook phonebook;

	bool quit = false;
	phonebook.load("yellowpages.txt");
	do {
		string n,p;
		switch ( get_input() ) {

    case '1': {
	    cout << "Name:";
	    cin.ignore(); 
	    getline(cin,n); 
	    cout << "Phone: ";
	    getline(cin,p); 
	    person pers(n,p); 
	    phonebook.insert(pers); 
	    cout << "Contact created.\n" << flush;	 
	    break;
			}
    case '3': {
	    cout << "Name: ";
	    cin.ignore(); 
	    getline(cin,n); 
	    person pers(n);
	    phonebook.remove(pers);
	    cout << "Contact deleted.\n"<< flush;
	    break;
			}
			// 
   case '2': {
	    cout << "Name: ";
	    cin.ignore(); 
	    getline(cin,n); 
	    person pers(n);
	    phonebook.find(pers);
	    break;
			}
			
    case '5': {
	    phonebook.print_inorder(); 
	    break;
			}
    case '4':
               cout << "Name: ";
			cin.ignore();
			getline(cin, n);
			cout << "Number: ";
			getline(cin, p);
			person pers(n, p);
			phonebook.edit(pers);
			cout << endl;
			break;

	case '6': {
	    quit = true;
		 phonebook.load("yellowpages.txt");
	    break;
			}
    default :
	    cout << '?' << endl;
		}
	} while ( !quit );
};
	char get_input() {
	string com;
	std::cout << "1:Create a Contact:  " << endl;
	std::cout << "2:Search for a Contact:  " << endl;
	std::cout << "3:Delete a Contact:  " << endl;
	std::cout << "4:Edit a Contact:  " << endl;
	std::cout << "5:Display all Contacts:  " << endl;
	std::cout << "6: Terminate program" << flush;
	cin >> com;
	return com[0];
};

I apologize I put the code between brackets but its not working is it b/c of the length?

I apologize I put the code between brackets but its not working is it b/c of the length?

Code tags syntax:

[code]

// paste code here

[/code]

or

[code=cplusplus] // paste code here

[/code]

Note that there are no spaces between the opening and closing bracket pairs. The second form adds C++ syntax highlighting and adds line numbers, which for something that long is very useful since you can refer to a line number.

The problem had nothing to do with the length of the code you posted.

Thanks, just forgot I guess. Anyways I figured what was wrong with my code and it is now fixed. Thanks again.

Glad you got it working. I fixed code tags for you. Could you maybe post what you were able to do to fix your code so it might be able to help others in the future?

Thanks CSCGal. Actually to fix it I had left out two braces, where Case 4 is. Any east fix and it took care of all of my errors. Simple mistake.

Actually to fix it I had left out two braces, where Case 4 is. Any east fix and it took care of all of my errors. Simple mistake.

volscolts16, do you test your code? 'Cuz there are some runtime error, when we add the number(after adding the name), Can you describe, how do you fixed it pleas.
Thank you

volscolts16 hasn't visited Daniweb for 2 years. You're not likely to get any answer in this thread.

You're not likely to get any answer in this thread.

it's pity, by the way, who can help me with this problem?

it's pity, by the way, who can help me with this problem?

Well, you could debug the code yourself. Or you could write your own BST instead of nicking one that's highly likely to be buggy.

Here's a tutorial with tested code that you can learn from (and steal, since it's all public domain). If you still have issues, I'd recommend starting a new thread with specific questions and noting any errors you're getting to help others help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.