How to delete

Pages: 12
How would I delete and element from an array. I have a structure that has all the info for the telephone directory. First name, Last name , zip , address.... I'm working on a function so If the user enters the phone number then that person will be deleted from the array. I'm first trying to get the hard parts of the code working so then I can come back and worry about the details.

This is what I got the for loop with nothing after it is where my brain farted.
Thanks
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
//Option D function 
void optionD( ifstream &fin, infoType tele[], int& size)
{		
	int index = 0;
	string dPhone;
	bool found = false;
	
	cout << tele[5].phone;
	cout << "You have selected option D " << endl;
	cout << "Please enter the phone number of the record to be deleted." << endl;

	cin >> dPhone;
	
	while( index < size && !found )
	{
		if(tele[index].phone.compare(dPhone)== 0)
		{
		cout << "The entry to be deleted is " << endl;
		cout << tele[index].lname << endl;
		cout << tele[index].fname << endl;
		cout << tele[index].streetAdd << endl;
		cout << tele[index].cityStateZ << endl;
		cout << tele[index].phone << endl;
		found = true;
		}
		
		index++;
	}
		
	//Deleting the element from Array
	for ( int i = 0; i < size; i++ )
	{



	}
	
	}
Hello.

We cannot erase a specific element from an array.

What people usually do(by calling std::vector::erase function if they are using std::vector) is to create another smaller array and copy all the data to the new array except one that you want to remove.

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
// Create and initialize new array
int* i = new int[10];
for( int j = 0; j < 10; ++j )
{
  i[j] = j;
}


// Remove third element
int* newArray = new int[9];
for( int j = 0; j < 2; ++j )
{
  newArray[j] = i[j];
}
for( int j = 2; j < 9; ++j )
{
  newArray[j] = i[j+1];
}

// Remove original array
delete[] i;

// i is now new Array
i = newArray;



Last edited on
OK, helpful. Thanks.
I"m wondering if I have an array that is declared as the type of my structure. I have to declare another array of that same type. But I'm wondering If I do like you say I don't know how big to make the size of the array that I'm going to initialize. How big do I make the size?

I"m not using std::vector. I'm so confused because I'm using a structure and its adding to my confusion because the array is that type.

1
2
3
4
5
6
7
8
9
//structure
struct infoType
{	
	string fname;
	string lname;
	string streetAdd;
	string cityStateZ;
	string phone;
};


This is my array
infoType tele[SIZE];
I don't know maybe this helps so you know.



Will I have to use a vector? If so can you use those with structures?
Last edited on
Hello.

When you cannot sure how many elements will be stored in your array, using vector is good idea because vector grows itself to store more data if it has to.

Passing struct to vector is totally legal.

You are basically creating brand new "type" by defining a struct.

Following is (almost) the same:
1
2
int a; // OK. Create a new "int" type variable named "a"
infoType b; // OK. Create a new "infoType" type variable named "b" 


Then notice that following is also the same:
1
2
std::vector<int> vi; // OK. Create vector that will store "int" type data
std::vector<infoType> vit;// OK. Create vector that will store "infoType" type data 


You can add elements like this:
1
2
3
4
5
int a = 3;
infoType b;

vi.push_back( a ); // vi is currently empty. Add "a" as a new element.
vit.push_back( b ); // vit is currently empty. Add "b" as a new element. 



You get elements like this:
1
2
vi[0]; // Return the first element of the vector to the expression.
vit[0]; // Return the first element of the vector to the expression. 
Last edited on
std::vector<int> vi; // OK. Create vector that will store "int type" data
std::vector<infoType> vit;


What is the vi and vit?
Thanks for answers
let me describe the statement again.

std::vector is just another brand new type that someone defined.

It just gets additional information(template arguments) contained in <>.
1
2
3
4
5
6

int                   i; // new "int" type variable "i"
infoType              it; // new "infoType" type variable "it" 
std::vector<int>      vi; // new "vector<int>" type variable "vi"
std::vector<infoType> vit; // new "vector<infoType>" type variable "vit"
Last edited on
OK,
Thanks.
So in programming outside the class room people use vectors more because they seem to work better, although I don't know I'm just guessing.
You store the number as you add items. Then you use that number when you want to delete a member from the array. You create a new array of n-1, then you copy the items from the old array into the new one. You can do this iteratively or use a function. You skip the position to be erased, delete it, then finish the array. You then decrement the count.
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
int Remove(unsigned int index)
{ 
     
     char* tarray  = new char[size-1)
     unsigned int i = 0;
     while(i != index)
     {
        tarray[i] = garray[i];
        i++;
      }

      for(i++; i < size; i++)
           tarray[i] = garray[i];

      delete[] garray;

      size -= 1;
      garray = new char[size];

      for(unsigned int j =0; j < size; j++)
          garray[j] = tarray[j];

      delete[] tarray;

       return size;
};


It's easier to use std::vector.

Last edited on
Your algorithm is long and maybe hard to read and understand. Any better solution everyone!? Simpler? Shorter? Safer? Smarter? :)
There's a trick to remove an element from an array. Note that you can do it when the order doesn't matter or you order/sort it afterwards:
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
//Option D function 
void optionD( ifstream &fin, infoType tele[], int& size)
{		
	int index = 0;
	string dPhone;
	bool found = false;
	
	cout << tele[5].phone;
	cout << "You have selected option D " << endl;
	cout << "Please enter the phone number of the record to be deleted." << endl;

	cin >> dPhone;
	
	while( index < size && !found )
	{
		if(tele[index].phone.compare(dPhone)== 0)
		{
		cout << "The entry to be deleted is " << endl;
		cout << tele[index].lname << endl;
		cout << tele[index].fname << endl;
		cout << tele[index].streetAdd << endl;
		cout << tele[index].cityStateZ << endl;
		cout << tele[index].phone << endl;

		// This removes the element from the array
		// But does not maintain the order of the elements
		--size;
		tele[index] = tele[size];
		// remove end

		found = true;
		}
		
		index++;
	}
...
}
That's good. No need to think too much.
I suggest @OP adding a boolean variable which only manages the item deleting.
Hope this helps.
OK, so I decided I would go with the idea posted by coder 77 and sort after.

I keep getting the strangest error. It is driving me nuts. I can't figure it out.

I don't know how to describe it. Its is not like a regular error that appears at the bottom during debug. You know how at the top of your screen you have tabs maybe one has input and the other is your actual program that is where the tab opens up. The tab says memcpy.asm

There is a box that appears also. This is what the box says
Unhandled exception at 0x5c1fc9c7 (msvcr100d.dll) in newnew.exe: 0xC0000005: Access violation reading location 0xcccccccc.

The box has 2 buttoms to break or continue.
I hope that describes it.
The is my input because I don't know where the issue is.
Bill Smith
9763 N. Adrian Hwy
Ann Arbor Mi,48719
517-260-9871
Lauren Wilt
8752 W. Munger Rd.
Tecumseh Mi, 48719
517-423-9826
Cory Tilton
9853 N. Territorial
Detroit Mi, 89915
313-987-5600
Walter Freeman
8752 W. Miles Rd.
Adrian Mi, 48719
517-423-7989
Jack Cowen
9853 Gabor ave.
Detroit Mi, 89915
313-987-2121

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const int SIZE = 5;

//structure
struct infoType
{	
	string fname;
	string lname;
	string streetAdd;
	string cityStateZ;
	string phone;
};

// Function to get info from file.
void readData(ifstream &fin, infoType tele[], int& size)
{	
	
	int i = 0;
	while(!fin.eof())
   {
		fin >> tele[i].fname;
		fin >> tele[i].lname;
		fin.get();
		getline(fin,tele[i].streetAdd, '\n');
		getline(fin,tele[i].cityStateZ, '\n');
		getline(fin,tele[i].phone, '\n');
	i++;
   }

	size = i;

}

//Function to display Menu
void displayMenu()
{
	cout << "Phone Directory Program " << endl;
	cout << endl;
	cout << "Options" << endl;
	cout << "[A]dd and entry to the phone directory. " << endl;
	cout << "[D]elete an entry from the phone directory. " << endl;
	cout << "[U]pdate an entry from the phone directory. " << endl;
	cout << "[L]ist the entire phone directory. " << endl;
	cout << "[E]xit the menu" << endl;
}

//Option A function
int optionA(ifstream &fin, infoType tele[], int& size)
{
	// Still need formatting
	cout << "----You have selected option A---- " << endl;
	cout << endl;
	cout << "Please enter the first name. " << endl;
	for(int i = 0; i < 1; i++)
	{
		cin >> tele[i].fname;
	}
	cout << "Please enter the last name. " << endl;
	for(int i= 0; i < 1; i++)	
	{
		
		cin >> tele[i].lname;
	}
	cout << "Please enter the street address. " << endl;
	cin.get();
	for(int i = 0; i < 1; i++)
	{
		getline(cin,tele[i].streetAdd); 
	}
	cout << "Please enter the city, state and zip code." << endl;
	for(int i = 0; i < 1; i++)
	{
		getline(cin,tele[i].cityStateZ); 
	}
	cout << "Please enter the phone number.(Example, 555-555-5555)" << endl;
	for(int i = 0; i < 1; i++)
	{
		cin >>tele[i].phone; 
	}
	size++;
	return size;

}

//Remove function
int remove( infoType tele[], int& size, int position_of_item_to_remove )
{
    for( int i = position_of_item_to_remove + 1 ; i < size ; ++i ) 
	tele[i-1] = tele[i] ;
    return --size;
}

//Option D function 
void optionD( ifstream &fin, infoType tele[], int& size)
{	
	int nCount = 0;
	int index = 0;
	string dPhone;
	bool found = false;
	infoType temp[SIZE];
	
	cout << "You have selected option D " << endl;
	cout << "Please enter the phone number of the record to be deleted." << endl;
	cout << "(Example, 555-555-5555)" << endl;

	cin >> dPhone;
	
	while( index < size && !found )
	{
		if(tele[index].phone.compare(dPhone)== 0)
		{
		
		cout << "The entry to be deleted is " << endl;
		cout << tele[index].lname << endl;
		cout << tele[index].fname << endl;
		cout << tele[index].streetAdd << endl;
		cout << tele[index].cityStateZ << endl;
		cout << tele[index].phone << endl;
		
		// This removes the element from the array
		// But does not maintain the order of the elements
		--size;
		tele[index] = tele[size];
		// remove end

		found = true;
		}
		
		index++;
	}
	





}
	
	




int main()
{
	ifstream fin("input.txt");
	
    int newSize = 10;
    infoType tele[SIZE];
		
   
	readData(fin, tele, newSize );
	newSize = optionA(fin, tele,newSize);
	optionD(fin,tele,newSize);
	
	
 //displayMenu();
 
  system("pause");
 
 return 0;

}


This is my entire code. Please any help is appreciated. I'm stumpted
I looked up memmove does anyone think that this could help?
I looked up memmove does anyone think that this could help?
No, the problem isn't remove.

your problem is on line 7: Your array has the capacy of 5 elements. But when I'm counting correctly you read 6. choose a greater number like 1000 or more.

By the way:
optionA() overwrites always the the first element (but increases size).
for(int i = 0; i < 1; i++) doesn't make sense it's not even a loop.

So your problem is that you don't check whether it's out of bounds.
OK,
I changed the array size to 10;
When you say it overwrites the first element but increases the size, I don't understand what I did there? If I do cout << size; before I begiin option d function the size is 6. So how come it is overwriting it?
The for loop think was because I was reading info into the array. My teacher said in order to do that you need to use a loop.
If its a 2 d array use 2 loops and so forth.
Can you explain how to tidy this up please. I thought option A was sitting good.Guess not
OK dude here is how I have tried to fix option A . I incremented the size before the loop that way there is an extra space. Will I still have this problem?
The array size took care of the errror thanks so much dude :) I appreciate the help
OK so I learned that that did not work. What happens is the thing that I added to the index is not there but also I'm missing the first listing which is supposed to be there so,

jack
lauren
walter

say I added kyle in option a function but I want to delete him in option d

so if i display the output after option d

I get
lauren
walter

So the first name is missing from before which is bad. But the name I wanted gone is gone.
I get into a hard time with arrays because I cant visualize what is actually happening
Thanks


This is the code as now

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const int SIZE = 10;

//structure
struct infoType
{	
	string fname;
	string lname;
	string streetAdd;
	string cityStateZ;
	string phone;
};

// Function to get info from file.
void readData(ifstream &fin, infoType tele[], int& size)
{	
	
	int i = 0;
	while(!fin.eof())
   {
		fin >> tele[i].fname;
		fin >> tele[i].lname;
		fin.get();
		getline(fin,tele[i].streetAdd, '\n');
		getline(fin,tele[i].cityStateZ, '\n');
		getline(fin,tele[i].phone, '\n');
	i++;
   }

	size = i;

}

//Function to display Menu
void displayMenu()
{
	cout << "Phone Directory Program " << endl;
	cout << endl;
	cout << "Options" << endl;
	cout << "[A]dd and entry to the phone directory. " << endl;
	cout << "[D]elete an entry from the phone directory. " << endl;
	cout << "[U]pdate an entry from the phone directory. " << endl;
	cout << "[L]ist the entire phone directory. " << endl;
	cout << "[E]xit the menu" << endl;
}

//Option A function
int optionA(ifstream &fin, infoType tele[], int& size)
{
	// Still need formatting
	size++;
	cout << "----You have selected option A---- " << endl;
	cout << endl;
	cout << "Please enter the first name. " << endl;
	for(int i = 0; i < 1; i++)
	{
		cin >> tele[i].fname;
	}
	cout << "Please enter the last name. " << endl;
	for(int i= 0; i < 1; i++)	
	{
		
		cin >> tele[i].lname;
	}
	cout << "Please enter the street address. " << endl;
	cin.get();
	for(int i = 0; i < 1; i++)
	{
		getline(cin,tele[i].streetAdd); 
	}
	cout << "Please enter the city, state and zip code." << endl;
	for(int i = 0; i < 1; i++)
	{
		getline(cin,tele[i].cityStateZ); 
	}
	cout << "Please enter the phone number.(Example, 555-555-5555)" << endl;
	for(int i = 0; i < 1; i++)
	{
		cin >>tele[i].phone; 
	}
	
	return size;

}

//Option D function 
void optionD( infoType tele[], int& size )
{	
	
	int index = 0;
	string dPhone;
	bool found = false;
	

	cout << "You have selected option D " << endl;
	cout << "Please enter the phone number of the record to be deleted." << endl;
	cout << "(Example, 555-555-5555)" << endl;

	cin >> dPhone;
	
	while( index < size && !found )
	{
		if(tele[index].phone.compare(dPhone)== 0)
		{
		
		cout << "The entry to be deleted is " << endl;
		cout << tele[index].lname << endl;
		cout << tele[index].fname << endl;
		cout << tele[index].streetAdd << endl;
		cout << tele[index].cityStateZ << endl;
		cout << tele[index].phone << endl;
		
		// This removes the element from the array
		// But does not maintain the order of the elements
		--size;
		tele[index] = tele[size];
		// remove end
		found = true;
		}
		else
		index++;
	}
	
	
	//To check the output

	for(int index = 0; index < size; index++)
	{
		cout << tele[index].lname << endl;
		cout << tele[index].fname << endl;
		cout << tele[index].streetAdd << endl;
		cout << tele[index].cityStateZ << endl;
		cout << tele[index].phone << endl;
	}




}


	
	

int main()
{
	ifstream fin("input.txt");
	
    int newSize = 10;
    infoType tele[SIZE];
		
   
	readData(fin, tele, newSize );
	newSize = optionA(fin, tele,newSize);
	optionD(tele,newSize);
	
	
 //displayMenu();
 
  system("pause");
 
 return 0;

}

Thanks
coder777 (2268)
You are definatly correct I put a loop to see what my output was inside option A. It is over writing the first index with the one that is added.
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
//Option A function
int optionA(ifstream &fin, infoType tele[], int& size)
{
	// Still need formatting
	size++;
	cout << "----You have selected option A---- " << endl;
	cout << endl;
	cout << "Please enter the first name. " << endl;
	for(int i = 0; i < 1; i++)
	{
		cin >> tele[i].fname;
	}
	cout << "Please enter the last name. " << endl;
	for(int i= 0; i < 1; i++)	
	{
		cin >> tele[i].lname;
	}
	cout << "Please enter the street address. " << endl;
	cin.get();
	for(int i = 0; i < 1; i++)
	{
		getline(cin,tele[i].streetAdd); 
	}
	cout << "Please enter the city, state and zip code." << endl;
	for(int i = 0; i < 1; i++)
	{
		getline(cin,tele[i].cityStateZ); 
	}
	cout << "Please enter the phone number.(Example, 555-555-5555)" << endl;
	for(int i = 0; i < 1; i++)
	{
		cin >>tele[i].phone; 
	}
	
	
	//To check the output
	// This loop tell me whatever is  entered in option A
	// is put in first positon and over writes the original. I want
	
	for(int index = 0; index < size; index++)
	{
		cout << tele[index].lname << endl;
		cout << tele[index].fname << endl;
		cout << tele[index].streetAdd << endl;
		cout << tele[index].cityStateZ << endl;
		cout << tele[index].phone << endl;
	}


	return size;

}


How can I add to this array.
If it is being overwritten and the first place how can I add to it and not over write it? Add to the array. The order is not important at this point. Other wise the array is overwritten in the first poistion.

Thanks


hm, you put that check mark. does that mean your issue is solved...

If it is being overwritten and the first place how can I add to it and not over write it?
the loops are needless, really:
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
//Option A function
int optionA(ifstream &fin, infoType tele[], int& size)
{
	// Still need formatting
	cout << "----You have selected option A---- " << endl;
	cout << endl;
if(size < SIZE) // this prevents the crash, since you have a very small array
{
	cout << "Please enter the first name. " << endl;
		cin >> tele[size].fname;
	cout << "Please enter the last name. " << endl;
		cin >> tele[size].lname;
	cout << "Please enter the street address. " << endl;
	cin.get();
		getline(cin,tele[size].streetAdd); 
	cout << "Please enter the city, state and zip code." << endl;
		getline(cin,tele[size].cityStateZ); 
	cout << "Please enter the phone number.(Example, 555-555-5555)" << endl;
		cin >>tele[size].phone; 
	size++;
}
	
	//To check the output
	// This loop tell me whatever is  entered in option A
	// is put in first positon and over writes the original. I want
	
	for(int index = 0; index < size; index++)
	{
		cout << tele[index].lname << endl;
		cout << tele[index].fname << endl;
		cout << tele[index].streetAdd << endl;
		cout << tele[index].cityStateZ << endl;
		cout << tele[index].phone << endl;
	}


	return size;

}
Pages: 12