Suggestions on my address book using map

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.

Thank you and have a great day!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <map>
using namespace 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; 
					}
					else if (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 31 is redundant. But the code seems ok to me.
@kbw

Cool. Thank you for taking the time to look at that :)

Best, Jae Kim
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.
Topic archived. No new replies allowed.