0
#import <iostream>
#import <string>
#include <fstream>

using namespace std;

//The phonebook itself.
class Book
{
	public:
	int people;
	
	//The information for a person.
	class Person
	{
		public:
		string firstname, lastname, phonenumber;
	};
	
	//Returns 1 if Person1 comes before Person2, 2 if Person2 comes before Person1, and -1 if they are the same.
	int Compare(Person Person1, Person Person2)
	{
		for(int i=0; i<Person1.lastname.length() && i<Person2.lastname.length();i++){
			if(Person1.lastname.at(i)<Person2.lastname.at(i))
				return 1;
			if(Person2.lastname.at(i)<Person1.lastname.at(i))
				return 0;
		}
		if(Person1.lastname.length()<Person2.lastname.length())
			return 1;
		if(Person2.lastname.length()<Person1.lastname.length())
			return 0;
		for(int i=0; i<Person1.firstname.length();i++) && i<Person2.lastname.length()
{
			if(Person1.firstname.at(i)<Person2.firstname.at(i))
				return 1;
			if(Person2.firstname.at(i)<Person1.firstname.at(i))
				return 0;
		}
		if(Person1.firstname.length()<Person2.firstname.length())
			return 1;
		if(Person2.firstname.length()<Person1.firstname.length())
			return 0;
		return -1;
	}
	
	struct Node
	{
		Node* parent;
		Node* left;
		Node* right;
		Person item;
	};
	
	Node* root;
	
	Book()
	{
		root=NULL;
	}
	
	//The interface of the phonebook.
	class UserInterface
	{
		
	};
	
	//Adds a person’s name (first and last) and phone number to the phone book.
	void Add(string firstname, string lastname, string phonenumber)
	{
		cout << "Adding carl"<<endl;
		Person carl;
		carl.firstname = firstname;
		carl.lastname = lastname;
		carl.phonenumber = phonenumber;
		people++;
		Node newperson;
		Node* parent;
		newperson.item=carl;
		if(root==NULL){
			newperson.parent=NULL;
			newperson.left=NULL;
			newperson.right=NULL;
			root = &newperson;
		}else{
			parent=ParentFinder(&newperson,root);
			if(Compare((&newperson)->item,parent->item){
				parent->left=&newperson;
			}else{
				parent->right=&newperson;
			}
		}
	}
	
	//Find which node should parent the new node.
	Node* ParentFinder(Node* kid, Node* areyoumymommy)
	{
		if(Compare(kid->item,areyoumymommy->item)==1){
			if(areyoumymommy->left!=NULL){
				return ParentFinder(kid, areyoumymommy->left);
			}else{
				return areyoumymommy;
			}
		}else if(Compare(kid->item,areyoumymommy->item)==0){
			if(areyoumymommy->right!=NULL){
				return ParentFinder(kid, areyoumymommy->right);
			}else{
				return areyoumymommy;
			}
		}
	}

	//Deletes a given person’s phone number, given only the name. 
	void Delete(string firstname, string lastname)
	{
		
		delete FindNodeName(firstname, lastname);
		people--;
	}
	
	//Finds the node for a given name.
	Node* FindNodeName(string firstname, string lastname)
	{
		Person nonum;
		Node* cur=root;
		nonum.firstname=firstname;
		nonum.lastname=lastname;
		nonum.phonenumber="555-5555";
		int a;
		while(a!=-1){
			a=Compare(nonum,cur->item);
			if(a==0){
				cur=cur->left;
			}else if(a==1){
				cur=cur->right;
			}
		}
		return cur;
	}

	//Locates a person’s phone number, given only the person’s name.
	string Find(string firstname, string lastname)
	{
		Node* pers=FindNodeName(firstname,lastname);
		return pers->item.phonenumber;
	}

	//Changes a persons phone number given the person’s name and new phone number.
	void Change(string firstname, string lastname, string phonenumber)
	{
		Node* pers=FindNodeName(firstname,lastname);
		pers->item.phonenumber=phonenumber;
	}

	//Displays (dumps) entire phone book in alphabetical order.  
	void Display(Node* rt)
	{
		ofstream phonebookout;
		phonebookout.open("Phonebook.txt");
		if (rt != NULL) {
           phonebookout << rt->item.lastname << ", " << rt->item.firstname << ":" << rt->item.phonenumber << "/n";      // Print the root item.
           Display( rt->left );    // Print items in left subtree.
           Display( rt->right );   // Print items in right subtree.
        }
		phonebookout.close();
	}

	//Quits the application, after first saving the phone book in a text file.
	void Quit()
	{
		Display(root);
		exit(1);
	}
};

int main()
{
	cout << "a" << endl;
	Book sefer=Book();
	if(sefer.root==NULL)
		cout << "b" << endl;
	sefer.Add("Josh", "Ellis", "941-7495");
	sefer.Add("Jo", "Ellis", "910-7495");
	sefer.Add("Xander", "Ellis", "922-7495");
	sefer.Add("Xander", "Ell", "922-9574");
	sefer.Add("Josh", "Xzqxiuz", "911");
	sefer.Add("Bobby", "Robertson", "902-7549");
}

Getting a seg fault during our add function. Its comparing the second persons name to itself. Can you help?

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by geojia
1

Try to find out on exactly which line the segfault occurs. Then check if the variables on that line have been initialized properly before the function is called.

0

sefer was declared but never initialized (you didn't allocate memory for it)
try using: Book serfer=new Book();

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.