Deleting an entry

A user will input an entry and will be stored in the memory. You can view all of your entries and replace each entry by editing it. My problem is i don't know how to delete an entry. A user will input the number of entry so it would be deleted. For example: 2... Then when you view it, the number 2 entry will be deleted and the number 3 entry will be number 2. In short, it will move forward.

example:
1 a
2 b
3 c

Select the number of entry you want to delete: 2

1 a
2 c

I don't know how to code/start it.
EDIT: I also know how to search though if you can ;o
Here's the code of mine without the delete code.
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
  #include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<iomanip>
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 line(char, int);
void newLine();

Person person[20];
Person p;
int index = 0;

int main()
{
	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)
{
	person[index] = p;
	index++;
}

void DisplayEntry()
{
	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;
		cout << i + 1 << setw(10);
		cout << person[i].fname << setw(10);
		cout << person[i].lname << setw(18);
		cout << person[i].address << setw(10);
		cout << person[i].cnum <<setw(10);
	}
	printf("\nRecords Total:%d\n", i);
	system("pause>0");
}

void EditEntry(Person *p)
{
	system("cls");
	system("COLOR A");
	int a = 1;
	cout << "Enter the entry number you would like to edit: ";
	cin >> a;
	newLine();
	if (a <= 100)
	{
		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-100.\n"
			<< "Please select this option again.\n\n";
	}

}


void line(char ch, int ctr)
{
	for (int i = 0; i < ctr; i++) {
		cout << ch;
	}
	cout << endl;
}

void newLine()
{
	char s; do{
		cin.get(s);
	} while (s != '\n');
}
Last edited on
You should use
vector<Person> person;
instead of
Person person[20];

In this case removing is doing by
person.erase(person.begin() + x);

Addition is doing by
person.push_back(p);

Displaying is doing by
for (int i = 0; i < person.size(); i++) {cout << person[i].fname << setw(10); ...}
or for (auto p = person.cbegin(); p != person.cend(); p++) {cout << p->fname << setw(10); ...}
or for(auto& p : person) {cout << p.fname << setw(10); ...}
my code is working with adding, viewing and editing. My only problem is the removing part.
person.erase(person.begin() + x); <<what part should i put it in my code?
Using a std::vector container will be much easier.

Aceix.
What's the use of vector?
it will allow you to maintain a collection of Person objects.
http://www.cplusplus.com/reference/vector/vector/

they are a lot 'nicer' to use than c-style arrays.
can i delete an entry without using vector since our professor won't ask us to use vector?
closed account (SECMoG1T)
if you can't use library facilities, maybe an option would be to use dynamic arrays, though it will still be quite a hassle to achieve your results.
Last edited on
So let's just say i'll use the array function... How can i start it by deleting it through entry numbers?
Do i have to use the same principle as I used in EditEntry?
closed account (SECMoG1T)
vectors, deques are really nice i agree with everybody around, all you would need is just calling erase .

If you want to switch to dynamic arrays , you'll have some added responsibilities , for example , freeing your memory, reallocating your array if you need to increase or decrease your size.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//i would suggest you make a function compare that returns true if two person objects 
//are equal and yet another swap function that can swap two person objects.

Person* prsnptr= new person[20];

void deleteperson(Person& personobj)
{
    for(std::size_t i=0;i<20;i++)
     {
        if(compare(prsnptr[i],personobj))
          {
              swap(prsnptr[19],prsnptr[i]);
              Person* newptr= new person[20];

              for(int k=0,k<19;k++)
                {newptr[k]=prsnptr[k];}

              delete  prsnptr;
              prsnptr=newptr;
         }
      else{}
     }
 }


such would work but would use up some of your pretty time.

Another approach i would recommend that would be applicable to static sized arrays is to make a function that would mark objects intended to be deleted as invalid, that way you wouldn't need unnecessary allocations,re allocations and keeping track of your memory, what would be necessary would cost you - adding a single boolean flag to your struct person.

1
2
3
4
5
6
7
8
9
10
11
12
void mark_invalid(Person& pobj)
 {
    for(std::size_t i=0;i<20;i++)
     {
        if(compare(person[i],pobj))
           {
              person[i].flag=true;
           }
      }
  }

when editing your records you can overwrite such an object, when printing , you would only print the valid objects.
Last edited on
when i use vector<Person> person;
it errors... how to fix it?
ahh there would be a lot of problem if i changed it to vector...
If i used the first approach, where should i put the Person* prsnptr= new person[20];

and what should i put it on top to initialize the void? Should be like this? void deleteperson(p& )
and this part
1
2
3
4
5
for(std::size_t i=0;i<20 //what's 20 means? the number of inputted entry?;i++)
     {
        if(compare//this one errors... should i input this?(prsnptr[i],personobj))
          {
              swap(prsnptr[19],prsnptr[i]);

sorry i'm completely noob at this
Last edited on
You can look up "linked lists"

Aceix.
closed account (SECMoG1T)
ahh there would be a lot of problem if i changed it to vector...
vectors would offer you the best performance and allow you to have bug free code.

where should i put the Person* prsnptr= new person[20];
on line 22.

what should i put it on top to initialize the void?
if you add it to your prototypes you would have to and an extra parameters for a Person* type array because your array person would be out of scope, it would have these form.
void deleteperson(Person*, Person& obj);

1
2
3
4
5
for(std::size_t i=0;i<20 //what's 20 means? the number of inputted entry?;i++)
     {                    //yes your array size, i referenced your line 22.
        if(compare//this one errors... should i input this?(prsnptr[i],personobj))
          {
              swap(prsnptr[19],prsnptr[i]);


20 -> is the size of your array, it isn't a constant you can change it to the size you are currently using.

compare(prsnptr[i],personobj),swap(prsnptr[19],prsnptr[i])
if you look at my previous post, i advised you to define the two functions as above
1. compare-> compares two Person objects and returns true if both are equal.
2. swap -> swaps two objects.

if you have correctly defined them they should work fine.

At last your code would have the following function prototypes with all functions defined.

1
2
3
4
5
6
7
8
void AddEntry(Person p);
void DisplayEntry();
void EditEntry(Person *);
void line(char, int);
void newLine();
void deleteperson(Person*, Person& obj);
void swap(Person& obj1, Person& obj2);
bool compare(const Person& obj1, const Person& obj2);



Last edited on
Without using vector:
1
2
3
4
5
6
7
8
9
void DelEntry()
{
int x;
cout << "Which to delete?";
cin >> x;
if (x >= index || x < 0) return;
for (int i = x; i < index - 1; i++) person[i] = person[i+1];
index--;
}
Last edited on
@andy1992
I tried what you did but Person* prsnptr= new // this errors person[20];
And this also:
if (//this errors too compare(prsnptr[i], personobj))
It says : Unexpected macro formal parameter list
wait... ahh i get it
should be
1
2
#define compare
#define swap 
Everything in strikethrough are error
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
#define compare(Person*, Person&obj);
#define swap(Person*, Person&obj);



Person* prsnptr = new person[20];




if (compare(prsnptr[i], personobj))
		{
			swap(prsnptr[19], prsnptr[i]);
			Person* newptr = new person[20];

			for (int k = 0, k<19; k++)
			{
				newptr[k] = prsnptr[k];
			}

			delete  prsnptr;
			prsnptr = newptr;
		}
		else{
			cout << "Invalid. Please try again.";
		
		}
Last edited on
This is my current code:
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
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<iomanip>
#define compare(Person*, obj);
#define swap(Person*, new);
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(Person*, Person& obj);
void line(char, int);
void newLine();
Person person[20];
Person p;
int index = 0;

int main()
{
	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(Person &obj);
				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)
{
	person[index] = p;
	index++;
}

void DisplayEntry()
{
	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;
		cout << i + 1 << setw(10);
		cout << person[i].fname << setw(10);
		cout << person[i].lname << setw(18);
		cout << person[i].address << setw(10);
		cout << person[i].cnum <<setw(10);
	}
	printf("\nRecords Total:%d\n", i);
	system("pause>0");
}

void EditEntry(Person *p)
{
	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(Person& pobj)
{
	if (compare(prsnptr[i], personobj))
	{
		swap(prsnptr[19], prsnptr[i]);
		Person* newptr = new person[20];

		for (int k = 0, k<19; k++)
		{
			newptr[k] = prsnptr[k];
		}

		delete  prsnptr;
		prsnptr = newptr;
	}
	else{
		cout << "Invalid. Please try again.";

	}
}

void line(char ch, int ctr)
{
	for (int i = 0; i < ctr; i++) {
		cout << ch;
	}
	cout << endl;
}

void newLine()
{
	char s; do{
		cin.get(s);
	} while (s != '\n');
}
Topic archived. No new replies allowed.