/* So the teacher built the main and a prototype for a class that builds and modifies a phone book. Right now I'm barely defining the class functions, but ran into trouble defining one of his prototypes "void erase" and "void find". I started on the void erase, which is supposed to find a person in the vector phone_book and erase it, while the "void find" simply locates the index of the desired string (name of the person being searched). I have listed the main the teacher built, the prototypes he gave us, and my work so far on defining them. You can find the two functions im asking about in the prototypes and my work. Please note that I'm not allowed to make any changes to the teachers main or prototypes. */
**the main:**
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include "phonebook.h"
using namespace std;
int main()
{
vector<Person> phone_book;
string name;
int number;
int answer;
srand((int)(time(0)));
phone_book.push_back(Person("Bruin, Joe", 5556456));
phone_book.push_back(Person("Simpson, Homer", 5557471));
phone_book.push_back(Person("Duffman, Barry", 5533331));
cout <<"\n";
cout << "Your phone book contains the following names and numbers: \n";
for (int i=0; i < phone_book.size(); i++)
{
phone_book[i].print();
cout << "\n";
}
cout <<"\n";
answer=0;
while (answer != 8)
{
cout << "\nChoose from the following options:\n\n";
cout << "1) Add people to the phone book.\n";
cout << "2) Erase a person from the phone book.\n";
cout << "3) Sort the phone book.\n";
cout << "4) Shuffle the phone book.\n";
cout << "5) Reverse the phone book.\n";
cout << "6) Print the phone book.\n";
cout << "7) Look up a person in the phone book.\n";
cout << "8) Quit.\n\n";
cin >> answer;
string clear;
getline(cin, clear);
if (answer == 1)
add_people(phone_book);
else if (answer == 2)
{
cout << "Enter a name: ";
getline(cin, name);
erase(phone_book, name);
}
else if (answer == 3)
sort(phone_book);
else if (answer == 4)
shuffle (phone_book);
else if (answer == 5)
reverse(phone_book);
else if (answer == 6)
{
cout <<"\n";
cout << "Your phone book contains the following names and numbers: \n";
print(phone_book);
}
else if (answer ==7)
{
cout << "Enter a name: ";
getline(cin, name);
int number = lookup(phone_book, name);
if (number > 0)
{
cout << "\n\nThe number for " << name << " is: " << number << "\n\n";
}
else
cout << name << " not found in the phone book.\n";
}
}
return 0;
}
**the prototypes:**
#ifndef PHONEBOOK_H
#define PHONEBOOK_H
#include <string>
#include <vector>
using namespace std;
class Person
{
public:
Person();
Person(string new_name, int new_phone);
string get_name() const;
int get_phone() const;
bool operator < (Person p) const;
void print() const;
private:
string name;
int phone;
};
void add_people(vector<Person> &phone_book);
void erase(vector<Person> &phone_book, string name);
void sort(vector<Person> &phone_book);
void shuffle(vector<Person> &phone_book);
void reverse(vector<Person> &phone_book);
void print(vector<Person> &phone_book);
int lookup(const vector<Person> &phone_book, string name);
#endif
**my work so far:**
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include "phonebook.h"
Person::Person()
{
name = "NONE";
phone = 0000000;
}
Person::Person(string new_name, int new_phone)
{
name=new_name;
phone=new_phone;
}
string Person::get_name() const
{
return name;
}
int Person::get_phone() const
{
return phone;
}
bool Person::operator < (Person p) const
{
return name < p.name;
}
void Person::print() const
{
cout << endl << name << " " << phone;
}
void add_people(vector<Person> &phone_book)
{
cout << "Please enter the new name: ";
string s;
getline(cin, s);
cout << "Please enter new number: ";
int number;
cin >> number;
phone_book.push_back(Person(s,number));
}
void erase(vector<Person> &phone_book, string name)
{
for(int i=0; i <= phone_book.size()-1; i++)
{
phone_book[i] = phone_book[i+1];
}
}
void sort(vector<Person> &phone_book)
{
}
void shuffle(vector<Person> &phone_book)
{
}
void reverse(vector<Person> &phone_book)
{
}
void print(vector<Person> &phone_book)
{
}
int lookup(const vector<Person> &phone_book, string name)
{
}