Addressbook(Edit and Delete)

Hi can i ask how to put Edit and Delete here!
i tried to but it only Delete or Edit the last entry :(
Please Help Thanks <3
im noob :(
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <stdio.h>

using namespace std;

class AddressBook{

public :
AddressBook()
{
count = 0;
}

void AddEntry();
void DisplayAll();
void DisplayEntry(int i); // Displays one entry
void EditEntry();
void DeleteEntry();
void SearchEntry();

int MainMenu();
struct Entry_Struct
{
char firstName[100] ;
char lastName[100] ;
char contact [100] ;
char address[100] ;
};

Entry_Struct entries[101];
unsigned int count;
};


void AddressBook::AddEntry()
{
cout << "\t\t\t\tEntry number " << (count + 1) << " : " << endl;

cout << "\t\t\t\tEnter First Name: ";
cin >> entries[count].firstName;

cout << "\t\t\t\tEnter Last Name: ";
cin >> entries[count].lastName;

cout << "\t\t\t\tEnter Contact Number : ";
cin >> entries[count].contact;

cout << "\t\t\t\tEnter Address(without Spaces): ";
cin >> entries[count].address;

++count; // tally total entry count
}

void AddressBook::EditEntry()
{
int num;
cout<<"\t\t\t\tEnter entry number:";
cin>>num;
--count;
if (num == count)
{
--count;
++count;
cout << "\t\t\t\tEntry number " << (count) << " : " << endl;
cout << "\t\t\t\tEnter First Name: ";
cin >> entries[count].firstName;
cout << "\t\t\t\tEnter Last Name: ";
cin >> entries[count].lastName;
cout << "\t\t\t\tEnter Contact Number : ";
cin >> entries[count].contact;
cout << "\t\t\t\tEnter Address: ";
cin >> entries[count].address;
}
}

void AddressBook::DeleteEntry()
{
int num;
cout<<"\t\t\t\tEnter entry number:";
cin>>num;
if (num == count)
{
--count;
cout<<"\t\t\t\tDeleted";
}

}


void AddressBook::DisplayEntry(int i)
{
cout << "\t\t\t\tEntry[" << i + 1 << "] : " << endl; // states # of entry
cout << "\t\t\t\tFirst name : " << entries[i].firstName << endl;
cout << "\t\t\t\tLast name : " << entries[i].lastName << endl;
cout << "\t\t\t\tContact number : " << entries[i].contact << endl;
cout << "\t\t\t\tAddress: " << entries[i].address << endl;
}

void AddressBook::DisplayAll()
{
cout << "\t\t\t\tNumber of entries : " << count << endl;

for(int i = 0;i < count;++i)
DisplayEntry(i);
}

void AddressBook::SearchEntry()
{
char lastname[100];
cout << "\t\t\t\tEnter last name : ";
cin >> lastname;

for(int i = 0;i < count;++i)
{
if(strcmp(lastname, entries[i].lastName) == 0)
{
cout << "\t\t\t\tFound ";
DisplayEntry(i);
cout << endl;
}
}
}


// Your addressbok
AddressBook my_book;

int MainMenu()
{
int num;
bool bQuit = false;
// Put all your code into a while loop.
while(bQuit == false)
{



cout << "\t\t\t\t+-------------------------------------+" << endl;
cout << "\t\t\t\t Address Book Menu " << endl;
cout << "\t\t\t\t " << endl;
cout << "\t\t\t\t 1- Add contact " << endl;
cout << "\t\t\t\t 2- Edit contact " << endl;
cout << "\t\t\t\t 3- Delete contact " << endl;
cout << "\t\t\t\t 4- View contacts " << endl;
cout << "\t\t\t\t 5- Search addressbook by last name " << endl;
cout << "\t\t\t\t 6- Exit " << endl;
cout << "\t\t\t\t " << endl;
cout << "\t\t\t\t +-------------------------------------+" << endl;

cout << endl;
cout << "\t\t\t\t Choose an Option: ";
cin >> num;
cout << endl;

if (num == 1)
my_book.AddEntry();
else if (num ==2)
my_book.EditEntry();
else if (num ==3)
my_book.DeleteEntry();
else if (num == 4)
my_book.DisplayAll();
else if (num == 5)
my_book.SearchEntry();
else if (num == 6)
bQuit = true;
else
cout << "\t\t\t\tInvalid choice. Please try again" << endl;

cout << endl;
}

return 0;
}

int main (){
MainMenu();
return 0;
}
If you pay attention to the assignment more, it would be pretty easy.
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
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

const int GLOBAL_SIZE = 150; // (*)

struct MyEntry
{
	char firstName[GLOBAL_SIZE];
	char lastName[GLOBAL_SIZE];
	char contact[GLOBAL_SIZE];
	char address[GLOBAL_SIZE];
};

bool addEntry(MyEntry myEntry[GLOBAL_SIZE], int &count)
{
	if(count >= GLOBAL_SIZE) return false;

	cout << "Entry [" << count + 1 << "] : " << endl;
	cout << "Enter first name : "; cin.getline(myEntry[count].firstName, sizeof(myEntry[count].firstName));
	cout << "Enter last name : "; cin.getline(myEntry[count].lastName, sizeof(myEntry[count].lastName));
	cout << "Enter contact : "; cin.getline(myEntry[count].contact, sizeof(myEntry[count].contact));
	cout << "Enter address : "; cin.getline(myEntry[count].address, sizeof(myEntry[count].address));

	count++;
	return true;
}

bool editEntry(MyEntry myEntry[GLOBAL_SIZE], int id, int count)
{
	id--;
	if(id < 0 || id >= count) return false;

	cout << "Enter first name : "; cin.getline(myEntry[id].firstName, sizeof(myEntry[id].firstName));
	cout << "Enter last name : "; cin.getline(myEntry[id].lastName, sizeof(myEntry[id].lastName));
	cout << "Enter contact : "; cin.getline(myEntry[id].contact, sizeof(myEntry[id].contact));
	cout << "Enter address : "; cin.getline(myEntry[id].address, sizeof(myEntry[id].address));

	return true;
}

bool displayEntry(MyEntry myEntry[GLOBAL_SIZE], int id, int count)
{
	id--;
	if(id < 0 || id >= count) return false;

	cout << "Entry [" << id + 1 << "] : " << endl;
	cout << "+ First name : " << myEntry[id].firstName << endl;
	cout << "+ Last name : " << myEntry[id].lastName << endl;
	cout << "+ Contact : " << myEntry[id].contact << endl;
	cout << "+ Address : " << myEntry[id].address << endl << endl;

	return true;
}

int searchEntry(MyEntry myEntry[GLOBAL_SIZE], string nameTarget, int count)
{
	int i;
	int N = 0;
	for(i = 0; i < count; i++)
	{
		if(std::string(myEntry[i].lastName) == nameTarget)
		{
			displayEntry(myEntry, i + 1, count); N++;
		}
	}

	return N;
}

bool deleteEntry(MyEntry myEntry[GLOBAL_SIZE], int id, int &count)
{
	int i;
	id--;
	if(id < 0 || id >= count) return false;
	for(i = id; i < count - 1; i++)
	{
		myEntry[i] = myEntry[i + 1];
	}
	count--;
	return true;
}

int main()
{
	int count = 0;
	MyEntry myEntry[GLOBAL_SIZE];

	int choice;
	int contact_id;
	bool bQuit = false;
	string lastName;
	char lastNameHolder[GLOBAL_SIZE];
	while(bQuit == false)
	{
		system("cls");
		cout << "+-------------------------------------+" << endl;
		cout << "Address Book Menu " << endl << endl;
		cout << "  1. Add contact " << endl;
		cout << "  2. Edit contact " << endl;
		cout << "  3. Delete contact " << endl;
		cout << "  4. View contacts " << endl;
		cout << "  5. Search addressbook by last name " << endl;
		cout << "  6. Exit " << endl;
		cout << "+-------------------------------------+" << endl;
		cout << "Number of contact records : " << count << endl; 
		cout << "Enter your choice : "; 
		
		if(cin >> choice && choice >= 1 && choice <= 6)
		{
			cin.ignore();
			switch(choice)
			{
				case 1 : 
				{
					if(!addEntry(myEntry, count))
					{
						cout << "+ The address book menu is full. Delete some contact record and try again." << endl;
					}
					else
					{
						cout << endl;
						cout << "The contact record has been successfully added to the list." << endl;
					}
				}
				break;
				case 2 :
				{
					cout << "Enter the contact id : "; 
					if(cin >> contact_id && (cin.ignore(), editEntry(myEntry, contact_id, count)))
					{
						cout << "The entry [" << contact_id << "] has been successfully updated." << endl;
					}
					else
					{
						if(!cin)
						{
							cin.clear();
							cin.ignore(1000, '\n');
						}		
						cout << "+ Invalid contact id or the contact record does not exist. Please try again." << endl;
					}
				}
				break;
				case 3 : 
				{
					cout << "Enter the contact id : "; 
					if(cin >> contact_id && (cin.ignore(), deleteEntry(myEntry, contact_id, count)))
					{
						cout << "The entry [" << contact_id << "] has been successfully removed." << endl;
					}
					else
					{
						if(!cin)
						{
							cin.clear();
							cin.ignore(1000, '\n');
						}	
						
						cout << "+ Cannot remove the entry num " << contact_id << ". Please try again." << endl;
					}
				}
				break;
				case 4 : 
				{	
					cout << "Enter the contact id : "; 
					if(cin >> contact_id && (cin.ignore(), displayEntry(myEntry, contact_id, count)))
					{
					
					}
					else
					{
						if(!cin)
						{
							cin.clear();
							cin.ignore(1000, '\n');
						}		
						cout << "+ Invalid contact id or the contact record does not exist. Please try again." << endl;
					}
				}
				break;
				case 5 : 
				{
					int searchCount;
					cout << "Enter the contact last name : "; 
					if(cin.getline(lastNameHolder, sizeof(lastNameHolder)) && (lastName = lastNameHolder, searchCount = searchEntry(myEntry, lastName, count)))
					{
						cout << "Found " << searchCount << " entries that match the last name" << endl;
					}	
					else
					{
						if(!cin)
						{
							cin.clear();
							cin.ignore(1000, '\n');
						}		
						cout << "+ The contact record you request does not exist. Please try again." << endl;
					}
				}
				break;
				case 6 : bQuit = true; break;
			}
		}
		else
		{
			if(!cin)
			{
				cin.clear();
				cin.ignore(1000, '\n');
			}
			cout << "+ Invalid choice. Please try again." << endl;
		}
	
		if(bQuit == false)
		{
			cout << "+ Please press any key to continue. . ."; cin.get();	
		}
	}

	cout << endl;
	cout << "Thank you for using the program. . ." << endl;
	cout << "Please press any key to continue. . ."; cin.get();	

	return 0;
}
Thanks you very much :)
so i need to pay more attention on assignments more
thank you for the tip :)


Topic archived. No new replies allowed.