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?

i have trouble trying to display the name of the contacts. p->first.get()

What is the error message your compiler is giving?

the compilers says "passing 'const name' as 'this' argument of 'std::string name::get()' discards qualifiers"

Change line 11 to: string get() const //get name . I think it stems from the nature of the strings once they enter in the map (since they become keys) but I am not sure.

the compilers says "passing 'const name' as 'this' argument of 'std::string name::get()' discards qualifiers"

I was just curious about the error message your compiler gives, anyway GCC gave a more sensible/straightforward message;

error: no matching function for call to 'name::get() const'

implying that it expects the get() member function to be const .


You could certainly add 'const correctness' to the program and figure out all the places where it is best to use const references;

// Not like this ...
bool operator<(name a, name b);
// but instead like this ...
bool operator<(const name & a, const name & b);

[EDIT]
Beaten by jonsca it seems ...

Oh. It works!! Hahaha, Thanks alot everybody :D

class name
{
    string name1;
    public:

    name(string s) //set name
    {
        name1 = s;
    }

    string get() const//get name            //added a 'const' and it works :D
    {
        return name1;
    }
};

// Define less than relative to name objects.
bool operator<(const name & a,const 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")));

    cout << "Contact List\n============\n";
    for(p = phonebook.begin(); p != phonebook.end(); p++)
        cout << "Name: " << p->first.get() << endl << "Number: " << p->second.get() << endl << endl;

    cout << "Find a contact by name : ";
    cin >> str;
    str[0] = toupper(str[0]);

    p = phonebook.find(name(str));
    if(p != phonebook.end())
    {
        cout << "\nContact Found!\n==============\n";
        cout << "Name: " << p->first.get() << endl << "Number: " << p->second.get() << endl << endl;
    }
    else
    cout << "Name not in directory.\n";

    return 0;
}

recently alot of my programs have been giving compiler errors because i failed to put a 'const' when needed =.=||

recently alot of my programs have been giving compiler errors because i failed to put a 'const' when needed =.=||

Perhaps read Const correctness

Ok now that the display is settled, how do i go about trying to search for a contact using the phone number?

This part doesnt work..

cout << "Find a contact by number : ";
    cin >> find_contact;
    //find_contact[0] = toupper(find_contact[0]);

    p = phonebook.find(phoneNum(find_contact));
    if(p != phonebook.end())
    {
        cout << "\nContact Found!\n==============\n";
        cout << "Name: " << p->first.get() << endl << "Number: " << p->second.get() << endl << endl;
    }
    else
    cout << "Contact is not in directory.\n";

the compiler says something like "no matching function to call to...."

@ this line -> "p = phonebook.find(phoneNum(find_contact));"

Use the iterator approach but this time if it->second equals the value you want, report it->first as the key. I don't think there's a built in way to search for key by value (you can probably find containers people have written-- like 2-way dictionaries -- out there).

ok Thanks Jonsca! Managed to get it running :D

class name
{
    string name1;
    public:

    name(string s) //set name
    {
        name1 = s;
    }

    string get() const//get name            //added a 'const' and it works :D
    {
        return name1;
    }
};

// Define less than relative to name objects.
bool operator<(const name & a,const name & b)
{
   return a.get() < b.get();
}

class phoneNum
{
    string str;
    public:

    phoneNum(string s) //set number
    {
      str = s;
    }

    string get() const //get number;
    {
      return str;
    }
};

int main()
{
    map<name, phoneNum> phonebook;
    map<name, phoneNum>::iterator p;
    string find_contact;
    int find_choice;

    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")));

    cout << "Contact List\n============\n";
    for(p = phonebook.begin(); p != phonebook.end(); p++)
        cout << "Name: " << p->first.get() << endl << "Number: " << p->second.get() << endl << endl;

    while(1)
    {
        cout << "Find a contact by\n(1)Name\n(2)Phone Number : ";
        cin >> find_choice;

        switch(find_choice)
        {
            case 1: cout << "Find a contact by name : ";
                    cin >> find_contact;
                    find_contact[0] = toupper(find_contact[0]);

                    p = phonebook.find(name(find_contact));
                    if(p != phonebook.end())
                    {
                        cout << "\nContact Found!\n==============\n";
                        cout << "Name: " << p->first.get() << endl << "Number: " << p->second.get() << endl << endl;
                    }
                    else
                    cout << "Contact is not in directory.\n";
                    break;

            case 2: cout << "Find a contact by phone number : ";
                    cin >> find_contact;

                    for(p = phonebook.begin(); p != phonebook.end(); p++)
                    {
                        if(p->second.get() == find_contact)
                        {
                            cout << "\nContact Found!\n==============\n";
                            cout << "Name: " << p->first.get() << endl << "Number: " << p->second.get() << endl << endl;
                        }
                    }
                    break;

            default: cout << "Invalid entry." << endl;
                     break;
        }
    }
    return 0;
}

:D:D:D

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.