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?
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;
}
}
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.