How to delete data from array.

I am trying to give the user the ability to delete data from a .txt file. In order to do this I have created a struct consisting of a string(phone number), a price (double), and whether or not the apartment is rented (string). The user should be able to delete a listing in the .txt file by using entering the phone number. For some reason my deleteList function is not working correctly. Can someone take a look at this for me?

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
 void deleteList(int& count) //write code to delete a listing next
{
	int placeFound;               // index where found
	string itemToDel;
	bool deleted;

	deleted = false;              // item not found and deleted yet

	cout << "What is the phone number for the listing you want to delete? ";
	cin >> itemToDel;
	cout << endl;

	// Find where itemToDel is in list
	placeFound = 0;                             // Start search at first index

	while ((placeFound < count + 1) &&           // While still values to search
		(array1[placeFound].phoneNum != itemToDel))     // and value not found
	{
		placeFound++;                        // increment index
		if (placeFound == count + 1)
		{
			cout << "Phone number not found.  Please try again: ";
			cin >> itemToDel;
			cout << endl;
			placeFound = 0;
		}
	}

	//If itemToDel was Found, delete it
	if (placeFound < count)
	{

		// Move all values below itemToDel UP one cell
		for (int num = placeFound + 1; num < count; num++)
		{
			array1[num - 1].phoneNum = array1[num].phoneNum;
			array1[num - 1].price = array1[num].price;
			array1[num - 1].vacancy = array1[num].vacancy;
		}

		// Decrement list size
		count--;

		deleted = true;
	}
}
closed account (E0p9LyTq)
If you are looking for the ability to delete cleanly array elements you might want to look at using an STL vector or list instead.

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

Yes, more coding involved using either a vector or list, but because of the dynamic allocation features inherent in the templates IMO makes for easier utilization.
Topic archived. No new replies allowed.