Hello! This is my first time using the map data structure. And I was wondering if you can suggest any improvements to my code? I know that what I wrote is not the best way that it could have been written and I would love your input on how to use map better.
#include <iostream>
#include <map>
usingnamespace std;
/*Implement a small address book program that allows users to enter names and email addresses, remove or change entries,
and list the entries in their address book. Don't worry about saving the address book to disk; it's ok to lose the data
when the program exits*/
int main ()
{
cout << "Enter '1' to add name and email address." << endl;
cout << "Enter '2' to delete an entry." << endl;
cout << "Enter '3' to change entry." << endl;
cout << "Enter '4' to see all the entires." << endl;
cout << "Enter '5' to end the program." << endl << endl;
int option = 0;
string name;
string email_address;
map <string, string> name_to_email;
cin >> option;
while (option != 5)
{
switch (option)
{
case 1:
cout << "Enter name or 'done' to go to another option: ";
cin >> name;
while (name != "done")
{
name_to_email[name];
cout << "Enter " << name << "'s " << "email address: ";
cin >> email_address;
name_to_email[name] = email_address;
cout << "Enter name or 'done' to go to another option: ";
cin >> name;
}
cout << endl;
break;
case 2:
if (name_to_email.empty ())
{
cout << "There is no name or email address to delete. Choose another option: " << endl;
break;
}
cout << "Enter name or 'done' to go to another option: ";
cin >> name;
while (name != "done")
{
name_to_email.erase (name);
cin >> name;
cout << "Enter name or 'done' to go to another option: ";
cin >> name;
}
cout << endl;
break;
case 3:
if (name_to_email.empty ())
{
cout << "There is no name to delete or email address to change. Choose another option: " << endl;
break;
}
cout << "Enter 'name' to change the name and enter 'email' to change the email address of an entry. " << endl;
cout << "Enter 'done' to go to another option: ";
cin >> name;
while (name != "done")
{
map <string, string>::iterator itr = name_to_email.find (name);
if (name == "name")
{
cout << "Enter name: ";
cin >> name;
string temp_email;
map <string, string> :: iterator itr = name_to_email.find (name);
temp_email = itr->second;
name_to_email.erase (name);
cout << "Enter a new name: ";
cin >> name;
name_to_email[name] = temp_email;
}
elseif (name == "email")
{
cout << "Enter name: ";
cin >> name;
map <string, string> :: iterator itr = name_to_email.find (name);
cout << "Enter new email address: ";
cin >> name;
itr->second = name;
}
cout << "Enter 'name' to change the name and enter 'email' to change the email address of an entry. " << endl;
cout << "Enter 'done' to go to another option: ";
cin >> name;
}
cout << endl;
break;
case 4:
for (map <string, string>::iterator itr = name_to_email.begin (), end = name_to_email.end ();
itr != end; ++itr)
{
cout << itr->first << ": " << itr->second << endl;
}
cout << endl;
break;
}
cout << "Enter '1' to add name and email address." << endl;
cout << "Enter '2' to delete an entry." << endl;
cout << "Enter '3' to change entry." << endl;
cout << "Enter '4' to see all the entires." << endl;
cout << "Enter '5' to end the program." << endl << endl;
cin >> option;
}
return 0;
}
Line 51 is wrong.
Line 68 doesn't make sense.
Line 75: Before you assign the email you should check whether the iterator is valid (i.e. != name_to_email.end())
Line 88: same as above.