Delete Entry

Desired Output:
address book
what would you like to do?
[1]add
[2]edit
[3]delete
[4]view
[5]search
[6]exit
please enter your choice: 4
.
.
.
No. FN Ln Add Cnum
1 a a a a
2 b b b b
3 c c c c
Records Total:3
.
.
.
address book
what would you like to do?
[1]add
[2]edit
[3]delete
[4]view
[5]search
[6]exit
please enter your choice: 3
.
.
.
Enter the entry number you want to delete: 2
[2] b b b b
Delete Selected entry? [y]/[n]y
Succesfully deleted
.
.
.
No. FN Ln Add Cnum
1 a a a a
2 c c c c

My Output:
address book
what would you like to do?
[1]add
[2]edit
[3]delete
[4]view
[5]search
[6]exit
please enter your choice: 4
.
.
.
No. FN Ln Address COntact no
1 a a a a
2 b b b b
3 c c c c
Records Total:3
.
.
.
address book
what would you like to do?
[1]add
[2]edit
[3]delete
[4]view
[5]search
[6]exit
please enter your choice: 3
.
.
.
Enter the entry number you want to delete: 2
[2] b b b b
Delete Selected entry? [y]/[n]y
Succesfully deleted
.
.
.
No. FN Ln Address Contact no
1 a a a a
2 b b b b
//this deletes the last inputted entry and does not delete the inputted entry you want to delete.. HELP!
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
  #include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<iomanip>
#include <fstream>
using namespace std;

struct Person
{
	char fname[100];
	char lname[100];
	char cnum[15];
	string address;
};

void AddEntry(Person p);
void DisplayEntry();
void EditEntry(Person *);
void DeleteEntry();
void SearchEntry();
void line(char, int);
void newLine();
Person person[20];
Person p;
int index = 0;

int main()
{
	ofstream fout;
	fout.open("c:\\csprog\\personinfo.txt");
	int option;
	while (1) {
		system("cls");
		system("COLOR D");
		cout << "-------------------Address Book";
		line('-', 19);
		cout << "\n           What would you like to do?\n";
		line('-', 50);
		cout << "                [1] Add Contact \n";
		cout << "                [2] Edit Contact \n";
		cout << "                [3] Delete Contact \n";
		cout << "                [4] View Contacts \n";
		cout << "                [5] Search Address Book \n";
		cout << "                [6] Exit \n";
		line('-', 50);
		cout << "Please enter your choice: ";
		cin >> option;
		newLine();
		char buff[2];
		switch (option)
		{
		case 1:
			system("cls");
			system("COLOR B");
			cout << "Enter First Name: ";
			cin.getline(p.fname, 100);
			cout << "Enter Last Name: ";
			cin.getline(p.lname, 100);
			cout << "Enter Address: ";
			cin >> p.address;
			newLine();
			cout << "Enter Contact Number: ";
			cin >> p.cnum;
			newLine();
			AddEntry(p);
			break;
			case 2:
				EditEntry(person);
				break;
			case 3:
				DeleteEntry();
				break;
		    case 4:
			     DisplayEntry();
			     break;
			case 5:
				SearchEntry();
				break;
		case 6:
			exit(1);
		default:
			cout << "Invalid choice \n\n";
			system("pause>0");
		}
	}
	return 0;
}

void AddEntry(Person p)
{
	ofstream fout;
	fout.open("c:\\csprog\\personinfo.txt");
	person[index] = p;
	index++;
}

void DisplayEntry()
{
	ofstream fout;
	fout.open("c:\\csprog\\personinfo.txt");
	system("cls");
	system("COLOR E");
	cout << setw(11) << "No." << setw(10)
		<< "   First Name" << setw(10)
		<< "   Last Name" << setw(18)
		<< "Address" << setw(10)
		<< "   Contact No." << setw(10);

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	int i = 0;
	for (; i < index; i++){
		cout << endl;
		fout << endl;
		cout << i + 1 << setw(10);
		fout << i + 1 << setw(2);
		cout << person[i].fname << setw(10);
		fout << person[i].fname << setw(2);
		cout << person[i].lname << setw(18);
		fout << person[i].lname << setw(2);
		cout << person[i].address << setw(10);
		fout << person[i].address << setw(2);
		cout << person[i].cnum << setw(10);
		fout << person[i].cnum << setw(2);
	}
	printf("\nRecords Total:%d\n", i);
	system("pause>0");
}

void EditEntry(Person *p)
{
	ofstream fout;
	fout.open("c:\\csprog\\personinfo.txt");
	system("cls");
	system("COLOR A");
	int a = 1;
	cout << "Enter the entry number you would like to edit: ";
	cin >> a;
	newLine();
	if (a <= index)
	{
		cout << endl;
		cout << "Please enter the updated information: ";
		cout << "\nEnter First Name: ";
		cin.getline(p[a - 1].fname, 100);
		cout << "Enter Last Name: ";
		cin.getline(p[a - 1].lname, 100);
		cout << "Enter Address: ";
		cin >> p[a - 1].address;
		newLine();
		cout << "Enter Contact Number: ";
		cin >> p[a - 1].cnum;
		newLine();
	}
	else{
		cout << "I'm sorry, that is an invalid selection.\n"
			<< "The number of entry is ranging from 0-" << index << " \n"
			<< "Please select this option again.\n\n";
		system("pause>0");
	}

}

void DeleteEntry()
{

	int entrynum, temp = 0;
	char choice;
	system("cls");
	cout << "\nEnter the Entry Number that you want to Delete: ";
	cin >> entrynum;

	temp = entrynum - 1;

	if (entrynum > index)
	{
		cout << "There are only " << index << " entries in the book";
		DeleteEntry();
	}
	else
	{
		cin.clear();
		cin.ignore();
		cout << "\n[" << temp + 1 << "]";
		cout << setw(15) << person[temp].fname;
		cout << setw(19) << person[temp].lname;
		cout << setw(19) << person[temp].address;
		cout << setw(17) << person[temp].cnum;
		cout << endl << endl;
		cout << "Delete Selected Entry? [Y]/[N]";
		cin >> choice;
		if (choice == 'y' || choice == 'n')
		{
			for (int i = temp; i < index; i++)
			{
				person[i].fname == person[i + 1].fname;
				person[i].lname == person[i + 1].fname;
				person[i].address == person[i + 1].fname;
				person[i].cnum == person[i + 1].fname;
			}
			--index;
			ofstream fout;
			fout.open("Address.txt");
			for (int i = 0; i < index; i++)
			{
				fout << person[i].fname << "," << person[i].lname << "," << person[i].address << "," << person[i].cnum << ",";
			}
			fout.close();
		}
	}

	cout << "\nSuccessfully Deleted!";
	cout << endl << endl;
	cout << "Go back to Main menu? [y]/[N]";
	cin >> choice;
	if (choice == 'y' || choice == 'Y')
		return;
	else if (choice == 'n' || choice == 'N')
	{
		DeleteEntry();
	}

	system("pause");
}

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
//this is the void DeleteEntry()
	int entrynum, temp = 0;
	char choice;
	system("cls");
	cout << "\nEnter the Entry Number that you want to Delete: ";
	cin >> entrynum;

	temp = entrynum - 1;

	if (entrynum > index)
	{
		cout << "There are only " << index << " entries in the book";
		DeleteEntry();
	}
	else
	{
		cin.clear();
		cin.ignore();
		cout << "\n[" << temp + 1 << "]";
		cout << setw(15) << person[temp].fname;
		cout << setw(19) << person[temp].lname;
		cout << setw(19) << person[temp].address;
		cout << setw(17) << person[temp].cnum;
		cout << endl << endl;
		cout << "Delete Selected Entry? [Y]/[N]";
		cin >> choice;
		if (choice == 'y' || choice == 'n')
		{
			for (int i = temp; i < index; i++)
			{
				person[i].fname == person[i + 1].fname;
				person[i].lname == person[i + 1].fname;
				person[i].address == person[i + 1].fname;
				person[i].cnum == person[i + 1].fname;
			}
			--index;
			ofstream fout;
			fout.open("Address.txt");
			for (int i = 0; i < index; i++)
			{
				fout << person[i].fname << "," << person[i].lname << "," << person[i].address << "," << person[i].cnum << ",";
			}
			fout.close();
		}
	}

	cout << "\nSuccessfully Deleted!";
	cout << endl << endl;
	cout << "Go back to Main menu? [y]/[N]";
	cin >> choice;
	if (choice == 'y' || choice == 'Y')
		return;
	else if (choice == 'n' || choice == 'N')
	{
		DeleteEntry();
	}

	system("pause");

I REMOVED THE void newLine,void line, and void SearchEntry since i reach the max length of char.
Last edited on
In your DeleteEntry():
1
2
3
4
5
6
7
8
9
		if (choice == 'y' || choice == 'n') // For both y and n?
		{
			for (int i = temp; i < index; i++) // Note: index - 1 due to the i + 1
			{
				person[i].fname = person[i + 1].fname; // Note: = (assign) instead of == (compare)
				person[i].lname = person[i + 1].fname;
				person[i].address = person[i + 1].fname;
				person[i].cnum = person[i + 1].fname;
			}


Do not call DeleteEntry(); recursively (especially on line 13) otherwise you will get more than expected. Use a loop instead
After hours of analyzing, i now know the main problem.
1
2
3
4
5
6
7
8
for (int i = temp; i < index; i++)
			{
				/*person[i].fname == person[i + 1].fname;
				person[i].lname == person[i + 1].lname;
				person[i].address == person[i + 1].address;
				person[i].cnum == person[i + 1].cnum;*/ //<--- even i remove this, it still has the same output. Meaning this doesn't have been executed. How to execute this code so it will be fixed?
				--index;
			}
1
2
3
4
5
6
7
8
9
if (choice == 'y' || choice == 'n') // For both y and n?
		{
			for (int i = temp; i < index; i++) // Note: index - 1 due to the i + 1
			{
				person[i].fname = person[i + 1].fname; // Note: = (assign) instead of == (compare)                                                                            
 //but it errors when i use =
				person[i].lname = person[i + 1].fname;
				person[i].address = person[i + 1].fname;
				person[i].cnum = person[i + 1].fname;

}

and what's the best replacement for DeleteEntry()?
Last edited on
I fixed the yes or no part
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
void DeleteEntry()
{

	int entrynum, temp = 0;
	char choice;
	system("cls");
	cout << "\nEnter the Entry Number that you want to Delete: ";
	cin >> entrynum;

	temp = entrynum - 1;

	if (entrynum > index)
	{
		cout << "There are only " << index << " entries in the book";
		DeleteEntry();
	}
	else
	{
		cin.clear();
		cin.ignore();
		cout << "\n[" << temp + 1 << "]";
		cout << setw(15) << person[temp].fname;
		cout << setw(19) << person[temp].lname;
		cout << setw(19) << person[temp].address;
		cout << setw(17) << person[temp].cnum;
		cout << endl << endl;
		cout << "Delete Selected Entry? [Y]/[N]";
		cin >> choice;
		if (choice == 'y' || choice == 'Y')
		{
			for (int i = temp; i < index - 1; i++)
			{
				person[i].fname == person[i + 1].fname;
				person[i].lname == person[i + 1].lname;
				person[i].address == person[i + 1].address;
				person[i].cnum == person[i + 1].cnum;
			}
			--index;
			ofstream fout;
			fout.open("c:\\csprog\\personinfo.txt");
			for (int i = 0; i < index; i++)
			{
				fout << person[i].fname << "," << person[i].lname << "," << person[i].address << "," << person[i].cnum << ",";
			}
			fout.close();
			cout << "\nSuccessfully Deleted!";
			cout << endl << endl;
		}
		else if (choice == 'n' || choice == 'N')
			cout << "Deletion has been cancelled...";
	}
	cout << "Go back to Main menu? [y]/[N]";
	cin >> choice;
	if (choice == 'y' || choice == 'Y')
		return;
	else if (choice == 'n' || choice == 'N')
	{
		DeleteEntry();
	}

	system("pause");
}
and what's the best replacement for DeleteEntry()? 

the best replacement for it is making a loop for delete entry instead of recursing it.
but it errors when i use =
I thought you would (better) use string.
So, to solve this:

strcpy(person[i].fname, person[i + 1].fname);

or, slower:
1
2
3
4
5
6
if (choice == 'y')
{
			for (int i = temp; i < (index - 1); i++)
			{
				person[i] = person[i + 1];
			}


For instance change this:
1
2
3
4
5
6
7
8
9
10
	cout << "\nEnter the Entry Number that you want to Delete: ";
	cin >> entrynum;

	temp = entrynum - 1;

	if (entrynum > index)
	{
		cout << "There are only " << index << " entries in the book";
		DeleteEntry();
	}
to
1
2
3
4
5
6
7
8
9
10
11
12
13
bool is_ok = false;
while(! is_ok)
{
	cout << "\nEnter the Entry Number that you want to Delete: ";
	cin >> entrynum;

	if (entrynum > index)
		cout << "There are only " << index << " entries in the book";
	else
		break;
}

	temp = entrynum - 1;
Thank you! It now works!
The only problem i had is my structure is a character and should be a string. It fixed everything and i don't have to change anything.
Topic archived. No new replies allowed.