Alright, I'm working on a project at the moment where I am trying to take a map and populate it with user input through a function. I under stand the basic way of populateing it by simply having something like...

typedef map< string, string> strMap;

strMap["key"] = 'value';

however, I want it to do something like...

typedef map< string, string> strMap;

int addMap(string Fname, string Lname)
{

//where Fname and Lname are strings entered by the user in main that hold someone first and last names respectively.

strMap[Fname] = Lname;

}

int main()
{

char Fname[20], Lname[20];
map m;

cout << "enter your first name: ";
cin.ignore();
cin.getline(Fname, 20);
cout << "enter your first name: ";
cin.ignore();
cin.getline(Lname, 20);
m.addMap(Fname, Lname);

}

does anyone know a way to accomplish this with a map?

Why not define a third parameter to your addMap function:

// I changed the return type to void because you don't use the return value
void addMap(strMap& mymap, string Fname, string Lname)
{   // where Fname and Lname are strings entered by the user
    // in main that hold someone first and last names respectively.
    mymap[Fname] = Lname;
}

And then you can add an element to the map m by calling addMap(m, Fname, Lname).

The question remains as to why you would be using char arrays instead of strings for Fname and Lname, but you didn't ask about that :-)

Edited 6 Years Ago by arkoenig: code cleanup

Thanks for the help, I will try that out and see how it works.

Sorry for the belated reply, I got sidetracked with another part of the project.

This article has been dead for over six months. Start a new discussion instead.