erase function to a map?

Hi,

I've made this map:
1
2
3
	struct person{string name1; string adress; string phonenumber;} data;

	map <string, person> phonebook;


I've search the internet for a code how to erase a specifik element from the map. What I found was this:
size_type erase( const key_type& key );

key_type& is tlfbog, right (phonebook&)? and key is this string from the map?

So I've made a key for my map called 'Peter James' and it has name1 = "Peter James", adress = "HappyRoad 333" and phonenumber = "123456".
But now I would like to erase it? and I have to type it from the keyboard, how to do?

I had something in mind like:
1
2
3
4
	string name2;
			cout << "Type the name of the person you would like to erase: ";
			getline(cin,str,'.');
			size_type erase( const phonebook& str );


But it doesn't work! I get these errors;
error C2065: 'size_type' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'erase'
error C2143: syntax error : missing ')' before 'const'
error C2059: syntax error : ')'
error C3861: 'erase': identifier not found

Best regards
/Malik
Last edited on
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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <map>
using std::map;
#include <string>
using std::string;

int main()
{
	char name[256];
	map<string, int> phonebook;
	
	phonebook["Peter James"] = 123456789;
	phonebook["James Peter"] = 111111111;
	
	map<string, int>::iterator myIter;

	cout << "Names and numbers\n";
	for(myIter = phonebook.begin(); myIter != phonebook.end(); myIter++)
		cout << myIter->first << " : " << myIter->second << endl;
	
	cout << "Enter the name to delete" << endl;
	cin.getline(name,256);
	myIter = phonebook.find(name);
	if(myIter != phonebook.end()){
		cout << "Deleting: " << name << " Phone: "<< phonebook[name] << endl;	
	        phonebook.erase(myIter);
        }
       else
               cout << "Sorry couldn't find: " << name << endl;
	
	cout << "Names and numbers\n";
	for(myIter = phonebook.begin(); myIter != phonebook.end(); myIter++)
		cout << myIter->first << " : " << myIter->second << endl;

	cout << name << " has been erased.\n";
	//More proof:
	//only the remaining phone number should display and the others should be 0;
	cout << phonebook[name] << endl //whatever was the user name
			<< phonebook["I do not exist"] << endl
			<< phonebook["Peter James"] << endl
			<< phonebook["James Peter"] << endl;

	return 0;
}



Jeff
Last edited on
Thank you for your help, I've tried 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
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <cstring>
#include <fstream>

using namespace std;

int main()
{
int choice=0;
struct person{
	string name1; 
	string adress; 
	string phonenumber;} data;

	map <string, person> phonebook;

hovedmenu:

	cout << "Press 1 to see list\nPress 2 to add a contact\nPress 3 to erase a contact\n";
	cin >> choice;
	switch (choice)
	{
	case 1:
	{
	map<string, person>::iterator print_iter;
	print_iter=phonebook.begin();
	while(print_iter != phonebook.end())
	{
	cout << print_iter-> second.name1 << " " << print_iter-> second.adress << " " << print_iter-> second.phonenumber << endl;
	print_iter++;
	}
	goto hovedmenu;
	}

	case 2:
	{
	cout <<"Type name: ";
	getline(cin,data.name1,'.');
	cout <<"Type adress: ";
	getline(cin,data.adress,'.');
	cout <<"Type phonenumber: ";
	cin >> data.phonenumber;
	phonebook.insert(pair<string, person>(data.name1,data));
	goto hovedmenu;
	}

	case 3:
	{
		map<string, person>::iterator eraseIter;
		string name;
		cout << "Enter the name to delete" << endl;
	getline(cin, name,'.');
	eraseIter = phonebook.find(name);
	if(eraseIter != phonebook.end())
		cout << "Deleting: " << name << " Phone: "<< phonebook[name] //-----------HERE IS THE ERROR------------------
<< endl;
	
	phonebook.erase(eraseIter);
	
	cout << "Names and numbers\n";
	for(eraseIter = phonebook.begin(); eraseIter != phonebook.end(); eraseIter++)
		cout << eraseIter-> second.name1 << " " << eraseIter-> second.adress << " " << eraseIter-> second.phonenumber << endl;

	cout << name << " has been erased.\n";
	}
	
	default:
	{
	cout << "Fail!" << endl;
	goto hovedmenu;
	}
	}
	cin.get();
}


I only got one error now:
-error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'main::person' (or there is no acceptable conversion)
Last edited on
it works now, this is my code for case 3:

I just removed the:
<< " Phone: "<< phonebook[name]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
case 3:
	{
		map<string, person>::iterator eraseIter;
		string name;
		cout << "Enter the name to delete" << endl;
	getline(cin, name,'.');
	eraseIter = phonebook.find(name);
	if(eraseIter != phonebook.end())
		cout << "Deleting: " << name << endl;
	
	phonebook.erase(eraseIter);
	
	for(eraseIter = phonebook.begin(); eraseIter != phonebook.end(); eraseIter++)
		cout << eraseIter-> second.name1 << " " << eraseIter-> second.adress << " " << eraseIter-> second.phonenumber << endl;

	cout << name << " has been erased.\n";
	}
	
	default:
	{
	cout << "Fail!" << endl;
	goto hovedmenu;
	}
Not bad Malik.
There was a logical error in my code.
Heres the right way to check if its ok to erase. Using my code as example:
1
2
3
4
5
6
if(myIter != phonebook.end()){
		cout << "Deleting: " << name << endl;
		phonebook.erase(myIter);
}
else
                cout << "Sorry couldn't find: " << name << endl;

You have just been bitten by the copy and paste bug.

:D

Cheers

Jeff
Last edited on
Good :) thank you for all your help Jeff.

I'm working on a project for my study, we are going to make a phonebook. So I might post some threads in here the next few days :)

Best regards
Malik, from Denmark.
Actually, how am I gonna make it to a function?

Something like
 
void erasePerson(string name, container <Person>& p)


I have to include a header-file and a cpp.file! I supose the container is struct I've made earlier? so I guess it should look like something like this:
 
void erasePerson(string name, <data>& p)

- since I've named my container (the struct) data?
Last edited on
You want to make a function within your struct or a global function?

You can use the case 3 in your loop to make a global function.
the declaration should be something like this;
using your example:
1
2
3
void erasePerson(string , map<string, person> &);
//in main
erasePerson(name, phonebook);

or if you use everything in case 3(some modifications still necessary), then you will prompt for a name inside the function that mean you dont need the name as a argument. Code translation:
1
2
3
void erasePerson(map<string, person> &);
//in main
erasePerson(phonebook);


Jeff
Last edited on
I think we are suposed to make a global function.
To be hornest I don't really understand much about the header-file and the cpp-file that should be included in the main-cpp-file.
I think its something like your first suggestion we are suposed to make :)
Both are global functions declarations and you already have everything for a definition.

Good Luck

Feel free to ask any other questions.

Jeff
Okay, thank you Jeff.

So my question is, what should I write in my .h and .cpp file?
Our teacher would like us to seperate statements in a .h-file and definitions in a .cpp-file :)

Yes the declarations goes in the header file .h and the definitions in the .cpp file
1
2
3
4
5
6
7
8
9
10
//the header file myfunctions.h
#ifndef MYFUCTIONS_H
#define MYFUCTIONS_H

// the declarations 
void foo1();
void foo2(int, int);
void foo3(int a, int b); //the compiler ignores a and b but its nice for documentation.

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//myfunctions.cpp
#include myfunctions.h

//definitions
void foo1()
{
//definition
}

void foo2(int age, int id)
{
//definition
}

void foo3(int a, int b);
{
//definition
}


have fun

Jeff
Okay, that's smart! So I can declare all my functions in the same header- and cpp-file :) smart move Jeff..

I'm still not sure about what to write in them, wether it should be pointers or operators. But I'll give it a try ;) thanks alot!
Oh i am not the author of that method thats just the way i was taught and its
very common most programmers do it this way(if not all)

Your teach never gave you a hint on how to do this?
aw...

anyway... Take a look at the preprocessor directives:
http://www.cplusplus.com/doc/tutorial/preprocessor.html

why i use it that way:
http://en.wikipedia.org/wiki/Include_guard

some prefer #pragma once
http://en.wikipedia.org/wiki/Pragma_once

Jeff
Last edited on
Thank you :) I'll look at it tomorrow, too tired now..
My teacher isn't good at being a techer at all.. else I wouldn't be here ;)
Topic archived. No new replies allowed.