map to function via pointer.

i'm have a hard time with a map that is passed to a function and then adding to that map, for whatever reason my book doesn't seem to cover this.

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
114
115
116
117
118
119
120
121
122
123
124
 #include <map>
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

void register_user ( map<string, string> *name_to_password );

string logged_in ()
{
    cout << "you're logged in!" ;
    while ( 1 )
        {

        string input;
        getline( cin, input, '\n' );
        cout << "Menu:" << endl << "1.change password" << "2.log out" ;
        getline( cin, input, '\n' );

        if ( input == "1" )
            {
            string new_password;
            cout << "Enter new password: ";
            cin >> new_password;


            cout << "New password has been set!";
            return new_password;


            }
        else if ( input == "2" )
            {
            cout << "you are now logged out!" << endl;
            break;
            }
        else
            {
            cout << "You must enter from one of the options in the menu " << endl;
            }
        }

}

void register_user ( map<string, string> *name_to_password )
{
    while ( 1 )
    {
        string username;
        cout << "To register we're going to need a user name...: " << endl;
        cin >> username;
        map<string, string>::iterator itfind = name_to_password->find( username );
        if ( itfind != name_to_password->end() )
            {
            cout << "sorry bud! that username has been taken. try another!" << endl << endl;
            }
        else
        {
            cout << "type in your password: " << endl;
            string password;
            cin >> password;
// this is where i'm getting an error, i've tried "*"before name_to_password.
            name_to_password[ username ] = password;
            cout << endl << "Register Successful! " << endl << endl;
            break;
        }
    }
}

int main ()
{

    map<string, string> name_to_password;
    name_to_password[ "danny" ] = "ramonesgghey1";

    int breaker = 1;

    while ( breaker == 1 )
    {
        string input;
        cout << "what would you like to do? ";
        cout << "1.log in" << "2.register";
        cin >> input;

        if ( input == "1" )
            {
                    string user_name_login;
                    getline( cin, user_name_login, '\n' );
                    cout << "enter your username: ";
                    getline( cin, user_name_login, '\n' );
                    map <string, string>::iterator itfind = name_to_password.find ( user_name_login );
                    if ( itfind != name_to_password.end() )
                    {
                        string password;
                        cout << "Enter password to log in: ";
                        cin >> password;
                        string map_password = itfind->second;
                        if ( map_password == password )
                        {
                        itfind->second = logged_in ();
                        }
                        else
                        {
                            cout << "wrong password entered!" ;
                        }
                    }
                    else if ( itfind == name_to_password.end() )
                    {
                        cout << "there is no entry with that username" ;

                    }
            }

        else if ( input == "2" )
            {
            register_user ( &name_to_password );
            }
        else
            {
                cout << "you must enter from one of the options in the menu: " ;
            }
    }
}
You need to dereference the name_to_password inside of parentheses.

(*name_to_password)[username]
Last edited on
You may consider to pass it as a reference:

1
2
3
4
5
6
7
8
void register_user ( map<string, string> &name_to_password ) // Note: &
{
...
        map<string, string>::iterator itfind = name_to_password.find( username ); // Note: . instead of ->
...
            name_to_password[ username ] = password;
...
}
Renthalkx97
Thanks that seemed to do the trick.

Coder777
Reference does seem like the better option but it's good to know both ways, isn't it ?

Thanks again guys.
It's personal preference really. Pointers and references basically both do the same exact thing except you can't reassign references like you can pointers.
Topic archived. No new replies allowed.