I am working on a phonebook application and I am having trouble implementing the add and find functions in the phonebook class. I am not sure how I am suppose to implement the functions using the prototypes provided. Any help would be appreciated.

#ifndef Name_H
#define Name_H

#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;


class Name 

{
    friend class PhoneBook;
    friend ostream &operator<<( ostream &, const Name & );
    friend istream &operator>>( istream &, Name & );
public:
Name(string name);

void setName(string n);
string getName() const;



protected:
string name;
};
#endif

#include <iostream>
#include <iomanip>
#include "Name.h"
using namespace std;

Name::Name(string n)
{
    name = n;
}

void Name::setName(string n)
{
    name = n;
}

string Name::getName() const
{
    return name;
}
ostream &operator<< ( ostream &output, const Name &name)
{
    output << name.name;
    return output;

}

istream &operator>>(istream &input, Name &name )
{

    char ws;
    input >> ws;
    getline(input,name.name);

}

#ifndef PhoneNumber_H
#define PhoneNumber_H
#include "Name.h"
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;

class PhoneNumber

{
    friend class PhoneBook;
    friend ostream &operator<<( ostream &, const PhoneNumber & );
    friend istream &operator>>( istream &, PhoneNumber & );
public:
PhoneNumber(string number);

void setNumber(string n);



protected:
string number;
};





#endif

#include <iostream>
#include <iomanip>
#include "PhoneNumber.h"
using namespace std;

PhoneNumber::PhoneNumber(string n)
{
    number = n;
}

void PhoneNumber::setNumber(string n)
{
    number = n;
}
ostream &operator<< ( ostream &output, const PhoneNumber &number)
{
    output << number.number;
    return output;

}

istream &operator>>(istream &input, PhoneNumber &number )
{

    char ws;
    input >> ws;
    getline(input,number.number);
}

#ifndef PhoneBook_H
#define PhoneBook_H
#include "PhoneNumber.h"
#include <iostream>
#include <string>
#include <stdexcept>

using namespace std;

class PhoneBook  
{
friend ostream &operator<<( ostream &, const PhoneNumber &);

public:
PhoneBook();


void add(const Name&, const PhoneNumber&);
const PhoneNumber& find(const Name&) const;

private:
    string aNames[ 1000 ];
    string aNumbers[ 1000 ];
};
#endif

#include <iostream>
#include <iomanip>
#include "PhoneBook.h"
#include "Name.h"
#include "PhoneNumber.h"
using namespace std;

ostream &operator<< ( ostream &output, const PhoneNumber &number)
{
    ios_base::fmtflags originalFormat = output.flags();
    output << left << setw(30) << Name::getName << number << endl;
    output.flags(originalFormat);

}

void PhoneBook::add (const Name &name, const PhoneNumber &number)
{

}

const PhoneNumber& find(const Name&) 
{

}


#include <iostream>
using namespace std;

#include "PhoneBook.h"

int main()
{
    PhoneBook phoneBook;

    bool quit = false;    // Flag to stop looping
    while (! quit)
    {
        // Output phone book menu
        cout << endl
            << "Phone Book Menu\n"
            << "1: add a listing\n"
            << "2: find a listing\n"
            << "3: print phone book\n"
            << "q: quit\n\n"
            << "Choice: ";

        // Input text typed by user
        string choice;
        cin >> ws;            // skip initial whitespace
        getline(cin, choice); // input user text

        Name name;
        PhoneNumber number;

        if (choice == "1")       // Add a listing
        {
            cout << "Add a Listing\n";
            cout << "Enter listing name: ";
            cin >> name;
            cout << "Enter listing phone number: ";
            cin >> number;
            phoneBook.add(name, number);
        }

        else if (choice == "2")  // Find a listing
        {
            cout << "Find a Listing\n";
            cout << "Enter name to find: ";
            cin >> name;
            cout << phoneBook.find(name) << endl;
        }

        else if (choice == "3")  // Print phone book
        {
            cout << "Print Phone Book\n";
            cout << phoneBook;
        }

        else if (choice == "q")  // Quit
        {
            cout << "Good-bye" << endl;
            quit = true;
        }

        else                     // Invalid user input
        {
            cerr << "Invalid choice: " << choice << endl;
        }
    }
}

Implementing the add() function is pretty simple, just assign one of the elements of aNames and aNumbers from the two parameters. For example: aNames[i] = name.getname();

Is class Phonebook right? The find() method is supposed to return a reference to PhoneNumber class, but PhoneNumber is not in Phonebook. I'm thinking aNumbers should be an array of PhoneNumbers, not an array of std::string. Either that, or find() should return a reference to std::string, not PhoneNumbers.

If all else fails, you can always add another level of abstraction; indeed, that what it looks like you intended, with the PhoneNumber class. Rather than declaring separate arrays for aName and aNumber, you should define the PhoneNumber class, and then in Phonebook you could declare an array (or better still, a vector) of PhoneNumber entries.

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.