Hi, I have spent way to much time stuck on this with no clue how to do it. My program is a phone book directory. I'm writing a funciton that when the user enters a phone number to be deleted the program will delete that corresponding entries info. So it will delete fname, lname, addresss...etc. I have an array. The array is of the type infoType (this is my structure name that so I can have fname....etc.)
I can't delete an entry I have to move it to the end of the array and someone suggested have it at the end this is there quote "Of course by a space I mean nothing as in array[0] = "";" I need help with this issue specificaly I joined this forum just because of it. Please help get this working. I don't even know how to do this. Please give specific examples. I have wasted 3 hours with nothing to show.
Thanks ,
J

//Option D function 
void optionD( ifstream &fin, infoType tele[], int& size)
{   

    int oldSize = 0;
    int index = 0;
    string dPhone;
    bool found = false;
    string temp = "";

    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++;
    }

    // I need the deletion so to speak to happen here.


}

You can't really remove an element from an array, all you can do is over-write it. The trick is keeping track of which elements can be over-written and which you want to keep! The best way to do this would be to make a second structure, which "wraps" the array of infoType objects. This wrapper would contain the array, but also a variable that indicates where the next free space in the array is. So, something like:

struct InfoTypeArray
{
    static const unsigned M_maxSize = 50;
    infoType M_tele[M_maxSize];
    unsigned M_size;

    InfoTypeArray() : M_size( 0 ) {}
};

Then, when you want to delete something, you can find the element to be deleted, as you do above, and then shuffle all the other elements back one position. Finally, the M_size should be decreased by one:

void Shuffle( InfoTypeArray& arr, unsigned start )
{
    // Shuffle the elements down
    for ( unsigned i = start + 1; i < arr.M_size; ++i )
        arr.M_tele[ i - 1 ] = arr.M_tele[i];

    // Decrement the size (since we just removed an element
    --M_size;
}

When you want to add something, you should then add it at the position indicated by M_size (unless M_size == M_maxSize) and then increment M_size by one.

Hope that helps :)

Incedentally, the best way to implelemnt all this functionality (adding, deleting, etc.) would be to turn the InfoTypeArray struct into a proper class, with private data and member functions for adding and deleting and fetching elements etc. However, I wasn't sure if you've covered this in you course yet. It's an improvement you should look into if you have time :)

OK Thanks I have some work to do.
Thanks you for the help I will keep you posted.

Hi, I keep getting this error so to speak my program compiles then it like gives me an error it goes to another tab the only thing I know how to tell you what it is like is if your string is out of range. (that is not the issue here though) But just to illustrate.

This is what comes up
Unhandled exception at 0x5c32bef0 (msvcr100d.dll) in newnew.exe: 0xC0000005: Access violation reading location 0xcccccccc.

This is my entire program I have spent hours !

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;

        //Remove 
        for(int i = index; i < size; i++ )
        {
            temp[nCount] = tele[i];
            nCount++;
        }

        for(int i = index; i < size; i++ )
        {
            tele[i] = temp[nCount];
            nCount--;
        }
            size--;
        found = true;
        }
    else

        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;

}

Option d appears to be the issue the last bit of code there is supposed to take a phone number entered by the user and find the info like fname , lname and such (the structure) and then get rid of it . Since you can't really delete it this is what I have
Please help it is so appreciated you have no idea how frusterated I am about this :)

Is there suppose to be a problem? Because, from what I see, everything works swell. Here's my output:

----You have selected option A---- 

Please enter the first name. 
John
Please enter the last name. 
Doe
Please enter the street address. 
Liberty
Please enter the city, state and zip code.
New York, New York, 11004–05
Please enter the phone number.(Example, 555-555-5555)
1-541-754-3010
You have selected option D 
Please enter the phone number of the record to be deleted.
(Example, 555-555-5555)
1-541-754-3010
The entry to be deleted is 
Doe
John
Liberty
New York, New York, 11004–05
1-541-754-3010

Since I don't have an input.txt file, I suggest you take a look into that file, and see if you misplaced some data.

Mine will work like that too expecpt that after line 22 in your outout the program then throws the error I told you about
This is the input
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
I see no issues with the input. Still get the error.
Thanks

Have you considered using a vector, instead of plain memory chunks?

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;

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

// Function to get info from file.
void readData(ifstream &fin, vector<infoType> &tele){
    while(!fin.eof()){
        struct infoType item;
        fin >> item.fname;
        fin >> item.lname;
        fin.get();
        getline(fin,item.streetAdd, '\n');
        getline(fin,item.cityStateZ, '\n');
        getline(fin,item.phone, '\n');
        tele.push_back(item);//appends the item to your vector.
   }
}

//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
void optionA(ifstream &fin, vector<infoType> &tele){
    // Still need formatting
    cout << "----You have selected option A---- " << endl;
    cout << endl;
    cout << "Please enter the first name. " << endl;
    infoType item;
    cin >> item.fname;
    cout << "Please enter the last name. " << endl;
    cin >> item.lname;
    cout << "Please enter the street address. " << endl;
    cin.get();
    getline(cin,item.streetAdd);
    cout << "Please enter the city, state and zip code." << endl;
    getline(cin,item.cityStateZ);
    cout << "Please enter the phone number.(Example, 555-555-5555)" << endl;
    cin >>item.phone;
    tele.push_back(item);
}

//Remove function
void remove( vector<infoType> &tele, int position_of_item_to_remove ){
    tele.erase(tele.begin()+position_of_item_to_remove);
}

//Option D function
void optionD( ifstream &fin, vector<infoType> &tele){
    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 < (int)tele.size() && !found ){
        struct infoType item=tele[index];
        if(item.phone.compare(dPhone)== 0){
        cout << "The entry to be deleted is " << endl;
        cout << item.lname << endl;
        cout << item.fname << endl;
        cout << item.streetAdd << endl;
        cout << item.cityStateZ << endl;
        cout << item.phone << endl;
        //Remove
        tele.erase(tele.begin()+index);
        found = true;
        }
        else
            index++;
    }
}

int main(){
    ifstream fin("input.txt");
    vector<infoType> tele;
    readData(fin, tele);
    optionA(fin, tele);
    optionD(fin,tele);
    return 0;
}

Yeah we havent learned them so I can;t

Someone told me it was my array size. Of 5 in main. Also they said there is issues with option A because I overwrite the first element in the array and then increment the size... What are your thoughts?
Thanks

Well, try it with your SIZE = 100. If you do have SIZE = 5 and you try to iterate (go through memory in your array), to an index which is for example 15 (generically spoken here, depending on how many items you have in your input.txt file, but more than your initial SIZE), you would access some parts of the memory which are restricted, thus the error.
As I said, modify the SIZE field to 100, and than try to run your program. I did, and it worked swell.

Yeah I just changed it to 10 since we are not dealing with large amount of info.
I'm havaing issue with my option a function and adding an element to the list. What happened is they way it was posted above it over wrote the first index with the new one. So i'm trying to find a way that I can add an element and keep my data the same.

So an example of how iit was before is

List before
jordan
smith
lauren

then I add kyle
kye
smith
lauren
SO the first optionw as over written. Not good

So i want it to be
kyle
jordan
smith
lauren

or whatever I 'm not concerened with order because i will sort it later i belive. But actually getting it to add this element is difficult. Here is my program under construction
Thanks for your help.

#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)
{
    size++;
    int index = 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[index+1].fname;

    cout << "Please enter the last name. " << endl;
    //for(int i= 0; i < 1; i++)  
    {
        cin >> tele[index].lname;
    }
    cout << "Please enter the street address. " << endl;
    cin.get();
    //for(int i = 0; i < 1; i++)
    {
        getline(cin,tele[index].streetAdd); 
    }
    cout << "Please enter the city, state and zip code." << endl;
    //for(int i = 0; i < 1; i++)
    {
        getline(cin,tele[index].cityStateZ); 
    }
    cout << "Please enter the phone number.(Example, 555-555-5555)" << endl;
    //for(int i = 0; i < 1; i++)
    {
        cin >>tele[index].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;

}

//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;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.