Question Regarding Map

Hello! I am new to map and the following code returns a "segmentation fault (core dumped) error message. I'm not sure why. Any suggestions on where I am going wrong and how to fix it?


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
#include <iostream>
#include <cstdlib>

#include <map>
using namespace std; 
/*Write a program with two options: register user and log in. Register user allows a new user to create a 
login name and password. Log in allows a user to log in and access a second area, with options for "change 
password" and "log out". Change password allows the user to change the password, and log out will return 
the user to the original screen.*/

void make_account (map<string,string>&account, string user_name, string password)
{
	account[user_name] = password;
}

int main ()
{	
	map<string, string>account;
	map<string, string>::iterator it = account.begin ();
	int option = 0; 
	string user_name; 
	string password; 
	cout << "Enter 1 to register user." << endl;
	cout << "Enter 2 log into your account." << endl;
	cout << "Enter: "; 
	cin >> option; 

	switch (option)
	{
		case 1:
		{
			cout << "Enter user name: "; 
			cin >> user_name; 
			cout << "Enter password: "; 
			cin >> password;  
			make_account (account, user_name, password);  
			cout << it->first; 
			break; 
		}
	}
	return 0; 
}

Last edited on
It works fine if you create the iterator (or reassign it) after you insert the first element into the map.
Topic archived. No new replies allowed.