Deleting from an array

This is a discussion on Deleting from an array within the C++ Programming forums, part of the General Programming Boards category; Greetings all from the land of Utterly Lost and Confused
this is my problem!
1. I created this an array ...

Deleting from an array

Greetings all from the land of Utterly Lost and Confused

this is my problem!
1. I created this an array of pointers to point to my PIM structure and made it a size 100.
2. I dynamically create each structure with new in my add function.
3. I added menu item that allows the user to select an entry for deletion.
4. When a element is deleted, make sure it is skipped when the print menu item is selected.

My Problem begins here, I enter 4 records, print the records to see all 4 records, I'm suppossed to delete the second record then reprint with the edited data.

It DOES NOT WORK I'm not understanding what I'm doing wrong.

I know its a little long but for the love of GOD someone Please help before my head explodes from thinking tooooooo hard.

This is the main of my program

/*************************************************
* This is my main Function for my PIM file. From here I call *
* my menu function. The Purpose of this program is to prompt *
* the user at the menu to either add to the contacts list or *
* display the contacts list or exit the program. I will do *
* this by setting up a loop to call various funtions to *
* perform these operations until the exit option is choosen. *
*************************************************/
#include "pim.hpp"

/*************************************************
* Welcome to My menu function, this is where I will print the*
* PIM menu to select which function to perform. My loop *
* process is begun here and will continue until the exit *
* option is selected from the menu. I am also using the NEW *
* operator to dynamically create each structure. This menu *
* calls the add_contact and the print_contact functions when *
* selected. The Exit command is also executed from here.
*************************************************/
#include "pim.hpp"
void menu(void)
{
pinfo * pfile[MAXRECS];
int choice, numrecs = 0;

/*************************************************
* This is the 1 switch statement if the user selects the add *
* option in the menu, the add_contact function is executed. *
* Once the function has completed it breaks out of the switch*
* and reprints the menu to allow the user to select his next *
* option.
*************************************************/
case 1:
CLS;
numrecs = add_contact(pfile);
break;

/*************************************************
* This is the 2nd switch statement if the user selects the *
* Print option in the menu, the print_contact function is *
* executed. Once the function has completed it breaks out of *
* the switch and reprints the menu to allow the user to *
* select his next option.
************************************************/
case 2:
CLS;
print_contact(pfile, numrecs);
break;

/*************************************************
* This is the 3rd switch statement, when the user selects *
* this option the remove contact function is called and *
* prompts the user to select the record number they wish to *
* remove from their contact list, then removes the record *
* they wish to have deleted.
*************************************************/
case 3:
//remove_contact(pfile);
break;

/*************************************************
* This is the 4rd switch statement, when the user selects *
* this option the loop is terminated and the program ends.
*************************************************/
case 4:
delete pfile;
return;
/*************************************************
* This is the default option incase the user input is not *
* within the range of the valid choices.
*************************************************/
default:
CLS;
menu();
}
}
}

/*************************************************
* Hey you made it to my add_contact function I will prompt*
* the user to tell me how many records he wishes to add, then*
* I set my loop limit to load my array based on user input *
* for number of records to add, I will output the current *
* record count and the max records allowed in my PIM file. *
* At this point I check to ensure that I do not exceed the *
* array limit. Now I'm ready to start getting my data from *
* the user to load my array. I continue to do this until I *
* reach my record loop limit. *
*************************************************/
int add_contact(pinfo *pfile)
{
CLS;

/*************************************************
* Woo Hoo Hey now we get to see if the array loaded correctly*
* From here my output headings are displayed then my loop is *
* initiated to output the array, I use cout.width to set *
* fixed field widths for my Name and my address fields. I *
* catch the return value of numrecs from add_function to set *
* my output loop limit. *
*************************************************/
void print_contact(pinfo *pfile,int &numrecs)
{

What would be a good way to remove the number three. You could shift everything in the array back... or... you could do this...

1 2 7 4 5 6

Think of how you can do that with your situation.

detete pfile[i]; is not the way you want to go.

>>>

cout << "How many Records do you wish to add ? ";
int numrecs=0;

<<<

Bad practice. Reverse these.

I am not going to go through all of it. Try to make an object of what you are trying to do. Call that object once you get it to work on the smaller scale. When you post code on the board use the code tags. [ code ] and [/ code ] without the spaces.

if I wish to delete element 2 from my array how do I do this? As I said I understand the logic, just not the syntax to do it. I have read 3 books so far and no examples have I been able to find. It's hard for me to understand unless I see a example and can walk myself thru it. thanks for any help you can give

1) add another member variable to the struct to act as a flag, say this:

int active = 1;

If the user wants to "delete" the record, which means you don't print it, then change the value of active to 0. In the print loop, you evaluate each elements active status with this:

if(array[i].active == 1)
//print.

That way you don't have to shift array items at all. This style seems to fit the requirement for #4 better than

2) you can shift array elements to fill in/overwrite the deleted element. If the element you want to delete has index i, then I think it's best to shift all elements one to the left, or down one. That is element with index i + 1 is rewritten to element with index i, element i + 2 is written to element i + 1, element i + 3 is written to element i + 2, etc, but every element less than i remains right where it is (index remains unchanged). Of course you need to know how many elements there are to know when to stop, but you can either keep a variable with the last used index to do that for you and change the variables value whenever you add or delete an element from the array, or you can calculate it using sizeof operator. Then when it comes time to print the values in the array you don't have to evaluate the flag, you just loop through and print whatever is there.