To move or not to move

Do you know how I can I move what I have located using my loop to the back of the array and have it be array[0] = ""; So when the array see's the space it just ignores it?
I'm a structure for the first time which seems to be making this harder to think about right now. If I put it after the loop how will I know what the index is? I'm trying to think of a general way because I will have different numebers that the user wants to be "DELETED" so to speak. How do I move the index to be deleted to the back of the array. and make it like arry[0] = ""; as was suggested to me. I don't understand thease ideas.
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
//Option D function 
void optionD( ifstream &fin, infoType tele[], int& size)
{	
	
        int index = 0;
	string dPhone;
	bool found = false;
	string temp = "";
	
	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;
		found = true;
		}
		
		index++;
	}
		
}
Something like this, perhaps:
1
2
3
4
5
6
7
8
// remove entry at position_of_item_to_remove
// by moving everything after it to the left by one position 
// and return new (logical) size of array
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] = tele[i-1] ;
    return --size;
}


And then:
1
2
3
4
5
6
7
8
9
10
11
12
13
// ...

if( tele[index].phone.compare(dPhone) == 0 )
{
    cout << "The entry to be deleted is " << endl;
    // ...
    
    // remove the entry at position index
    int new_size = remove( tele. size, index ) ;
    found = true;
}

//... 
I tried it, I and it compiles but then I got some crazy error after

Unhandled exception at 0x5c1dcafa (msvcr100d.dll) in newnew.exe: 0xC0000005: Access violation writing location 0xcccccccc.
I don't get this at all.
Thanks
@ JLBorges (1293)
Please think carefully about this algorithm. In fact it's not easy.
The easy (if slightly glib) answer is: if you want to change the order of objects in a container, or remove objects, then an array isn't the best choice. You'd be much better off using a linked list.

In fact, you're better off altogether using an STL container class, such as std::list.

http://www.cplusplus.com/reference/stl/list/

JLBorges, I think you've gotten things the wrong way around in your remove function - you're actually shifting everything to the right. The statement in your for loop should be:
tele[i-1] = tele[i]
I posted an another solution (Page 3) :
http://www.cplusplus.com/forum/general/85585/

That's more clear.
Last edited on
> JLBorges, I think you've gotten things the wrong way around in your remove function -
> you're actually shifting everything to the right. The statement in your for loop should be:
> tele[i-1] = tele[i]

Yes, my mistake. Thanks.
Last edited on
Topic archived. No new replies allowed.