Hi everyone!
I've been working on this code for 2 days(got half of it from the web)
class name
{
string name1;
public:
name(string s) //set name
{
name1 = s;
}
string get() //get name
{
return name1;
}
};
// Define less than relative to name objects.
bool operator<(name a, name b)
{
return a.get() < b.get();
}
class phoneNum
{
string str;
public:
phoneNum(string s) //set number
{
str = s;
}
string get() //get number;
{
return str;
}
};
int main()
{
map<name, phoneNum> phonebook;
map<name, phoneNum>::iterator p;
string str;
phonebook.insert(pair<name, phoneNum>(name("Jasmine"),phoneNum("99991111")));
phonebook.insert(pair<name, phoneNum>(name("Catherine"),phoneNum("99992222")));
phonebook.insert(pair<name, phoneNum>(name("Johnson"),phoneNum("55553333")));
phonebook.insert(pair<name, phoneNum>(name("Teressa"),phoneNum("44448888")));
for(p = phonebook.begin(); p != phonebook.end(); p++)
cout << "Name: " << p->first.get() << " " << "Number: " << p->second.get() << endl;
cout << "Find a contact by name : ";
cin >> str;
str[0] = toupper(str[0]);
p = phonebook.find(name(str));
if(p != phonebook.end())
cout << "Phone number: " << p->second.get();
else
cout << "Name not in directory.\n";
return 0;
}
Basically its a phonebook with objects that have names and phone numbers in it (did by mapping 1 object to another object). However i have trouble trying to display the name of the contacts. p->first.get() doesnt work although p->second.get() works.
My lecturer said that i shouldnt map an object to another object or it'll be complicated to try to get the value inside the object variable.
So how can i go about modifying the code so that i'm able to output the name of the contacts?