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;
}
}
}