Phonebook directory

Hey, i need help with my phonebook directory, my code is fine and working perfectly, but i need help adding the name "Firstname, lastname". my code only accepts one name. and i also have to search the contact using lastname. i think i can use Map<String,List<String>> for that but i need help with adding two names.

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
// Phonebook directory Program

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

void printline(char, int);
bool name_valid(string);
bool mob_valid(string);

class contact
{
    string name;
    string mob;

    public:
    	//Initialize the contact by a default value
        contact(): name(""), mob("")
        {}

        // Shows all contacts
        bool show()
        {
            if(name != "")
            {
                cout << name << "\t" << mob << endl;
                return 1; //Indicates success
            }
            else
                return 0; //Indicates failure
        }

        //Search
        bool show(string search_term)
        {
            if(search_term == name)
            {
                cout << name << "\t" << mob << endl;
                return 1;
            }
            else
                return 0;
        }

        //Checks whether the name exists or not
        bool name_exists(string tname)
        {
            if(tname == name)
                return 1;
            else
                return 0;
        }

        //The contact object is initialized by valid values
        bool add(string new_name, string new_mob)
        {
            if(name=="")
            {
                name = new_name;
                mob = new_mob;
                return 1; // Success
            }
            else
                return 0; // Failure

        }

        //Edits the contact details
        bool edit(string);

        //Sets the contact details to default values
        //That is, the contact details are thus erased
        bool erase(string new_name)
        {
            if(new_name==name)
            {
                name = "";
                mob = "";
                return 1;
            }
            else
                return 0;
        }
};


//Edits the contact
bool contact :: edit(string new_name)
{
    string new_mob;
    if(new_name==name)
    {
        cout << "Enter new name: "; cin >> new_name;
        cout << "Enter new mobile no: "; cin >> new_mob;

        name = new_name;
        mob = new_mob;
        return 1;
    }
    else
        return 0;
}



int main()
continues

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
{
    contact person[100];

    string temp_name, temp_mob;
    int choice, i, counter;
    bool flag;
    bool cancel_flag;

    cout << "**** PHONEBOOK ******" << endl;


    do
    {
        cout << "\n\n\n";
        printline('-', 20);
        cout << "0. Show contacts" << endl
        << "1. Add Contact" << endl
        << "2. Edit Contact" << endl
        << "3. Delete Contact" << endl
        << "4. Search" << endl
        << "5. Quit" << endl << endl
        << "Your choice...";
        cin >> choice;

        system("cls");
        printline('-', 20);
        cancel_flag = 0;
        flag = 0;
        counter = 0;

        switch(choice)
        {
            case 0:
                cout << "Showing Contacts" << endl;
                printline('-', 20);

                for(i=0; i<100; i++)
                    if(person[i].show())
                        flag = 1;

                if(!flag)
                    cout << "No contacts found!" << endl;
                break;

            case 1:
                cout << "Add New Contact\t\t\t\tpress $ to cancel" << endl;
                printline('-', 20);
                counter = 0;

                //Loop until correct name and mobile number are entered
                do
                {
                    flag = 0;
                    if(counter)
                        cout << "Try again\t\t\t\tpress $ to cancel"
						<< endl;

                    //counts how many times the do-while loop executes
					counter++;

                    cout << "Name: "; cin >> temp_name;

                    //Cancel operation
                    if(temp_name=="$")
                    {
                        cancel_flag = 1;
                        break;
                    }
                    cout << "Mobile No.: "; cin >> temp_mob;

                    //Cancel operation
                    if(temp_mob=="$")
                    {
                        cancel_flag = 1;
                        break;
                    }

                    //Check whether name exists
                    for(i=0; i<100; i++)
                        if(person[i].name_exists(temp_name))
                        {
                            cout << "The name you entered is already there"
							"in the phonebook, enter a different name."
							<< endl;
                            flag = 1;
                            break;
                        }

                }while(!name_valid(temp_name) ||
								flag ||
						!mob_valid(temp_mob));

                if(cancel_flag)
                {
                   system("cls");
                    break;
                }


                //This code adds the contact to phonebook
                for(i=0; i<100; i++)
                    if(person[i].add(temp_name, temp_mob))
                    {
                        cout << "Contact added successfully!" << endl;
                        flag = 1;
                        break;
                    }

                if(!flag)
                    cout << "Memory full! Delete some contacts first."
					<< endl;
                break;

            case 2:
                cout << "Enter a contact name to edit:"
				"\t\t\t\tpress $ to cancel\n";
				 cin >> temp_name;

                //Cancel Operation
                if(temp_name=="$")
                {
                   system("cls");
                    break;
                }

                for(i=0; i<100; i++)
                    if(person[i].edit(temp_name))
                    {
                        cout << "Edited Successfully!" << endl;
                        flag = 1;
                        break;
                    }

                if(!flag)
                    cout << "Contact name not found!" << endl;
                break;

            case 3:
                do
                {
                    if(counter)
                        cout << "Try again" << endl;
                    counter++;
                    cout << "Enter a contact name to delete:"
					"\t\t\tpress $ to cancel\n";
					cin >> temp_name;

                    //Cancel Operation
                    if(temp_name=="$")
                    {
                        system("cls");
                        break;
                    }


                    //Final Confirmation
                    for(i=0; i<100; i++)
                    if(person[i].name_exists(temp_name))
                    {
                        flag = 1;
                        cout << "Are you sure you want to delete? (1/0)"
						<< endl;
                        int yes;
                        cin >> yes;
                        if(!yes)
                        {
                           system("cls");
                            cancel_flag = 1;
                        }
                        break;
                    }

                    if(!flag)
                        cout << "Contact name not found!" << endl;

                    if(cancel_flag)
                        break;

                    // This code deletes the contact
                    if(flag)
                    {
                        for(i=0; i<100; i++)
                            if(person[i].erase(temp_name))
                            {
                                cout << "Deleted successfully!" << endl;
                                break;
                            }
                    }

                }while(!flag);
                break;

            case 4:
                do
                {
                    if(counter)
                        cout << "Try again" << endl;
                    counter++;
                    cout << "Search a name: \t\t\t\tpress $ to cancel\n";
					 cin >> temp_name;

                    //Cancel Operation
                    if(temp_name=="$")
                    {
                       system("cls");
                        break;
                    }

                    for(i=0; i<100; i++)
                        if(person[i].show(temp_name))
                        {
                            flag = 1;
                            break;
                        }

                    if(!flag)
                        cout << "Contact name not found" << endl;
                }while(!flag);

                break;

            case 5:
                return 0;
                break;

        }
    } while(1);

//    getch();
    return 0;
}

//prints a line
void printline(char ch, int size)
{
    for(int i=0; i<size; i++)
        cout << ch;
    cout << "\n";
}


//Contact name validation
bool name_valid(string tname)
{

    if(tname.size()>20)
    {
        cout << "Invalid Name!\nEnter a name within 20 characters!"
		<< endl;
        return 0;
    }
    else if(tname == "")
    {
        cout << "Invalid Name!\nName cannot be blank!" << endl;
        return 0;
    }
    else
        return 1;
}

//mobile number validation
bool mob_valid(string tmob)
{
    if(tmob.size()>13 || tmob.size()<10)
    {
        cout << "Invalid mobile no.\nEnter a no."
		"between 10 and 13 digits" << endl;
        return 0;
    }
    else if(tmob == "")
    {
        cout << "Invalid mobile no.\nMobile"
		"no cannot be blank" << endl;
        return 0;
    }
    else
        return 1;
}
// Phonebook directory Program

#include <iostream>
#include <string>
using namespace std;

void printline(char, int);
bool name_valid(string);
bool mob_valid(string);

class contact
{
string name;
string lastn;
string mob;

public:
//Initialize the contact by a default value
contact() : name(""), lastn(""), mob("")
{}

// Shows all contacts
bool show()
{
if (name != "")
{
cout << name << "\t" << lastn << "\t" << mob << endl;
return 1; //Indicates success
}
else
return 0; //Indicates failure
}

//Search
bool show(string search_term)
{
if (search_term == name)
{
cout << name << "\t" << lastn << "\t" << mob << endl;
return 1;
}
else
return 0;
}

//Checks whether the name exists or not
bool name_exists(string tname, string lname)
{
if (tname == name && lname == lastn)
return 1;
else
return 0;
}

//The contact object is initialized by valid values
bool add(string new_name, string new_lastn, string new_mob)
{
if (name == "")
{
name = new_name;
lastn = new_lastn;
mob = new_mob;
return 1; // Success
}
else
return 0; // Failure

}

//Edits the contact details
bool edit(string new_name, string new_lastn);

//Sets the contact details to default values
//That is, the contact details are thus erased
bool erase(string new_name, string new_lastn)
{
if (new_name == name && new_lastn == lastn)
{
name = "";
lastn = "";
mob = "";
return 1;
}
else
return 0;
}
};



*********************************************************
I updated your class a little ... Left the int main() for you to do. For the most part all you have to do is update the main to accept the last name etc...

1. There is no reason to have stdlib.h .. it's the same as using namespace std;
2. All you had to is add "last name" and implement it every time your ask for first name.
btw .... You should optimize your code and fix your class format if you're in the mood..... anyway good luck and I hope this helps..
thank you! i did and it's giving me error at //Check whether name exists i try to put temp_lastn but it's still giving me error. unfortunately my pc doesnt allow me to view what my error is. and i used the #include stdlib for "cls" (clear screen without system)
nevermind, i see my error. thanks
Topic archived. No new replies allowed.