The book Im using. Pg 108 - 124
Objects Abstraction, Data Structures and Design using C++
Author: Elliot Koffman and Paul Wolfgang

I copied the example from the book, but the code is not working. I got a lot of errors
in Phone_Directory.cpp. I dont know what to do

//Directory_entry.h
#pragma once
#include<string>
#include<iostream>
using namespace std;

class Directory_Entry
{

private:
	//Data fields
	string name;
	string number;

public:
// Constructors
	  Directory_Entry(string n, string num):
	  name(n), number(num)
	  {}
	  // Default Constructor
	  Directory_Entry(): name(""), number("") {}

	//Modifiers
	void set_number(const string& new_number)
	  {
		  number = new_number;
	  }
	  //Accessor
	  string get_name() const {
		  return name;
	  }
	  string get_number() const {
		  return number;
	  }

	  	 inline friend ostream& operator<<(ostream& os ,const Directory_Entry& per)
{
os << "Given Name: " << per.name << '\n' 
<< "Number: " << per.number << '\n' ;
	return os;
}
//Phone_Directory.h
#pragma once
#include<string>
#include"Directory_Entry.h"
using namespace std;
class Phone_Directory
{
private:
	
	// Directory Entry Definition req
	int find(const string& name) const;
	void add(string& name, const string& number);
	void remove_entry(int index);
	void reallocate();
	int size;
	int capacity;
	Directory_Entry* the_directory;
	string source_name;
	bool modified;

public:
	Phone_Directory(void);
	~Phone_Directory(void);
	void load_data(const string& source_name);
	string lookup_entry(string& name) const;
	string add_or_change_entry(const string& name, const string number);
	string remove_entry(const string& name);
	void save();






};
//Phone_Directory.cpp
#include "StdAfx.h"
#include "Phone_Directory.h"
#include "Directory_Entry.h"
//using namespace std;


void Phone_Directory :: load_data(const string& source_name)
{
	this->source_name = source_name;
	ifstream in(source_name.c_str());
	if(in) {
		string name;
		string number;
		while(getline(in,name)) {
			if(getline(in, number))
			{
				add(name,number);
			}
		}
		in.close();
	}
}



string Phone_Directory:: add_or_change_entry(const string& name, const string& number)
{
	string old_number = "" ;
	int index = find(name);
	if(index != -1) {
		old_number = the_directory[index].get_number();
		the_directory[index].set_number(number);
	}
	else{
		add(name,number);
	}
	modified = true;
	return old_number;
}

string Phone_Directory :: lookup_entry(const string& name) const {
	int index = find(name);
	if(index != -1) {
		return the_directory[index].get_number();
	}
	else{
		return "";
	}
}

void Phone_Directory :: save() {
	if(modified){
		ofstream out(source_name.c_str());
		for(int i = 0; i < size i++)
		{
			out << the_directory[i].get_name() << endl;
			out << the_directory[i].get_number() << endl;
		}
		out.close();
		modified = false;
	}
}

int Phone_Directory :: find(const string& name) const {
	for(int i = 0; i < size; i++) {
		if(the_directory[i].get_name() == name)
		return i;
	}
	return -1;
}

void Phone_Directory :: add(const string& name, const string& number) {
	if(size == capacity)
		reallocate();
	the_directory[size] = Directory_Entry(name,number);
	size++;
}
void Phone_Directory::reallocate() {
	capacity *=2;
	Directory_Entry* new_directory = new Directory_Entry[capacity];
	for(int i = 0; i< size; i++)
	{
		new_directory[i] = the_directory[i];
}

	delete[] the_directory;
	the_directory = new_directory;
}
// PD_Application.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Phone_Directory.h"
#include "Directory_Entry.h"
#include<iostream>
#include<istream>
#include<ostream>
#include<limits>
using namespace std;

void process_commands(Phone_Directory&);
void do_add_change_entry(Phone_Directory&);
void do_lookup_entry(Phone_Directory&);
void do_remove_entry(Phone_Directory&);
void do_save(Phone_Directory&);

int _tmain(int argc, char* argv[])
{
	if(argc < 2) {
		cerr << "Must specify the name of the data file"
			<< " that contains the directory|n" ;
		return 1;
	}
	Phone_Directory the_directory;
	the_directory.load_data(argv[1]);
	process_commands(the_directory);
}

void process_commands(Phone_Directory& the_directory)
{
	string commands[] = {
		"Add/Change Entry",
		"Look Up Entry",
		"Remove Entry",
		"Save Directory",
		"Exit" };
		const int NUM_COMMANDS = 5;
		int choice = NUM_COMMANDS -1;
		do
		{
			for(int i = 0; i< NUM_COMMANDS; i++)
			{
				cout << "Select: " << i << " " << commands[i] << "\n";
			}
			cin >> choice;
			cin.ignore(numeric_limits<int>::max(), '\n');
			switch(choice) {
			
			case 0: do_add_change_entry(the_directory); break;
			case 1: do_lookup_entry(the_directory); break;
			case 2: do_remove_entry(the_directory); break;
			case 3: do_save(the_directory); break;
			}
		}while(choice < NUM_COMMANDS - 1);
}
void do_add_change_entry(Phone_Directory& the_directory) {
	string name;
	string number;
	cout << "Enter name:  " ;
	getline(cin, number);
	string old_number = the_directory.add_or_change_entry(name,number);
	if(old_number != " ") {
		cout << name << "has been changed in the directory \n" ;
		cout << "Old number was " << old_number << "\n";
	}
	else{
		cout << name << "has been added to the directory\n";
	}
}
void do_look_up_entry(Phone_Directory& the_directory) {
	string name;
	cout << "Enter name: " ;
	getline(cin, name);
	string number = the_directory.lookup_entry(name);
	if(number != " ")
	{
		cout << "The number for " << name << " is" << number << "\n";
	}else {
		cout << name << " is not in the directory\n";
	}
}

void do_remove_entry(Phone_Directory& the_directory)
{
	// Excercise
}

void do_save(Phone_Directory& the_directory){
	the_directory.save();
}

Can you try compiling the code again and giving us the errors?

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.