Search value in a map with user input

Hi, y'all! I'm stuck with the following problem: I want to get the name of an employee by user input and then put in structure, but I can't figure out how to do this properly, so when I assign a value of some employee just like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  map <const char*, int> Employees = {
		{ "Albert W.", 4 },
		{ "Chris R.", 1 },
		{ "Rebecca C.", 2 },
		{ "Jill V.", 3 },
		{ "Leon K.", 6 },
		{ "Mr D.", 9 } };

  const char *str;
  while (!Employees.count(str)) {
    cout << "Enter employee name:";
    str = "Jill V.";

  ...
  Node* p = new Node;
  p->name = Employees.find(str)->first;
  p->data = Employees.find(str)->second;
}


it works fine, but when I trying to make an opportunity to input name by the user my while loop doesn't breaks when I enter an existing value in a map, it seems like he doesn't check "str" variable at all. Also, I get an error "map/set iterator not dereferencable" when trying to insert element into structure after while loop:

1
2
3
4
5
6
7
8
9
10
11
  const int size = 20;
  const char *str;
  while (!Employees.count(str)) {
	cout << "Jill V. found" << Employees.count("Jill V.") << endl; // 1
	cout << "Dummy found" << Employees.count("dummy") << endl; // 0
	cin.getline(str, size); // enter "Jill V" but while loop continue executing
	}
  ...
  Node* p = new Node;
  p->name = Employees.find(str)->first;
  p->data = Employees.find(str)->second;


cout << Thanks in advance for any help and suggestions you can provide" << endl;
Last edited on
replace const char* with std::string as the key-value-type in the map<...>

comparing 2 char pointers will always fail, until the 2 pointer point to the same memory address, even if the 2 different char pointers point to the same text / content

comparing 2 std::strings on the other hand will give you the desired result
Last edited on
Topic archived. No new replies allowed.