Help with creating a dynamic array that can add and delete elements

This is a discussion on Help with creating a dynamic array that can add and delete elements within the C++ Programming forums, part of the General Programming Boards category; Hey Im working on this program for school, pretty much I have to create an array of five strings and ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    14

    Help with creating a dynamic array that can add and delete elements

    Hey Im working on this program for school, pretty much I have to create an array of five strings and create and use two functions, addEntry and deleteEntry. These should allow you to change the size and add elements to the array or emulate the functions of a vector as the book says. So far I have the addEntry working but when I tried to put in my deleteEntry I now get an error saying the .exe has stopped working. Can you help me out? I muct have made the deleteEntry Function wrong or something.

    Here's my code:

    Code:
    //COT 3002
    //The purpose of this program is to create a function that can add or remove elements to an array
    
    #include <iostream>
    #include <cstring>
    #include <string>
    using namespace std;
    
    //Function Prototypes
    string* addEntry(string *dynamicArray, int &size, string newEntry);
    string* deleteEntry(string *dynamicArray, int &size, string entryToDelete);
    
    int main()
    {
    	int size=5;
    	string *strPtr=new string[size];
    	int i=0;
    	strPtr[i++] = "Frink";
    	strPtr[i++] = "Wiggum";
    	strPtr[i++] = "Nahasapeemapetilon";
    	strPtr[i++] = "Quimby";
    	strPtr[i++] = "Flanders";
    	cout<<"Initial List:"<<endl;
    	for(i=0; i < size; i++) 
    		cout << i << ": " << strPtr[i] << endl;
    	cout<<"\nAfter Adding an Entry:"<<endl;
    	strPtr=addEntry(strPtr, size,"Spuckler");
    	for(i=0; i < size; i++) 
    		cout << i << ": " << strPtr[i] << endl;
    	strPtr=deleteEntry(strPtr, size, "Nahasapeemapetilon");
    	for(i=0; i < size; i++) 
    		cout << i << ": " << strPtr[i] << endl;
    
    
    	return 0;
    }
    
    string *deleteEntry(string *dynamicArray, int &size, string entryToDelete)
    {// create a new dynamic array 1 element larger than dynamicArray
       string *newArray = new string[size - 1];
    
       // copy all elements from dynamicArray into new array
       for(int i = 0; i < size; i++)
       {
           if(dynamicArray[i]!=entryToDelete)
    		   newArray[i] = dynamicArray[i];
       }
    
       size--;
    
       // delete dynamicArray
       delete [] dynamicArray;
    
       // and return the new array
       return newArray;
    }
    
       
    
    string *addEntry(string *dynamicArray, int &size, string newEntry)
    {
       // create a new dynamic array 1 element larger than dynamicArray
       string *newArray = new string[size + 1];
    
       // copy all elements from dynamicArray into new array
       for(int i = 0; i < size; i++)
       {
           newArray[i] = dynamicArray[i];
       }
    
       // add the new entry onto the end of the new array
       newArray[size] = newEntry;
    
       // increment size
       size++;
    
       // delete dynamicArray
       delete [] dynamicArray;
    
       // and return the new array
       return newArray;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    You're overflowing your array.

    > if(dynamicArray[i]!=entryToDelete)
    > newArray[i] = dynamicArray[i];
    Once you've gone past the deleted element, the index to newArray is one LESS than the index subscripting dynamicArray
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Thank you, I now changed the code of that function to this:
    Code:
    string *deleteEntry(string *dynamicArray, int &size, string entryToDelete)
    {	int stop;
    	// create a new dynamic array 1 element smaller than dynamicArray
       string *newArray = new string[size - 1];
    
       // copy all elements from dynamicArray into new array
       for(int i = 0; i < size - 1; i++)
       {
           if(dynamicArray[i]!=entryToDelete)
    		   newArray[i] = dynamicArray[i];
    	   else 
    	   {stop=i;
    	   for(int i = stop; i < size-1; i++)
    		   newArray[i] = dynamicArray[i+1];}
       }
    
       size--;
    
       // delete dynamicArray
       delete [] dynamicArray;
    
       // and return the new array
       return newArray;
    }
    I no longer get the error, but I now have a new problem. It deletes the entry and put the next entry in place of it like I want it to, but then prints that same entry one more time, like if had i had 12345 before and want to delete 2, I have 1334, when I want 1345. Please help me out.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Nevermind I fixed it, I used this code:
    Code:
    string *deleteEntry(string *dynamicArray, int &size, string entryToDelete)
    {	int stop;
    	// create a new dynamic array 1 element smaller than dynamicArray
       string *newArray = new string[size - 1];
    
       // copy all elements from dynamicArray into new array
       for(int i = 0; i < size - 1; i++)
       {
           if(dynamicArray[i]!=entryToDelete)
    		   newArray[i] = dynamicArray[i];
    	   else 
    	   {stop=i;
    	   break;}}
    	 for(int i = stop; i < size-1; i++)
    		   newArray[i] = dynamicArray[i+1];
    
    
       size--;
    
       // delete dynamicArray
       delete [] dynamicArray;
    
       // and return the new array
       return newArray;
    }
    Thank you for your help

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Sorry, but your indentation needs a lot of work. For example, it would have been easier to read if you had written the function like this:
    Code:
    string* deleteEntry(string* dynamicArray, int& size, string entryToDelete)
    {
        int stop;
        // create a new dynamic array 1 element smaller than dynamicArray
        string* newArray = new string[size - 1];
    
        // copy all elements from dynamicArray into new array
        for (int i = 0; i < size - 1; i++)
        {
            if (dynamicArray[i] != entryToDelete)
                newArray[i] = dynamicArray[i];
            else
            {
                stop=i;
                break;
            }
        }
    
        for (int i = stop; i < size - 1; i++)
            newArray[i] = dynamicArray[i + 1];
    
        size--;
    
        // delete dynamicArray
        delete [] dynamicArray;
    
        // and return the new array
        return newArray;
    }
    However, there is one major problem with your implementation of deleteEntry: what happens if none of the strings in the dynamic array are equal to entryToDelete?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Yeah I know thats true, I realized that and tried to fix it by moving the size-- into the else statement but that messed everything else up, I'm not to sure how to fix it.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    Unless your assignment gives you specific instructions about what to do when the entry to delete is not present, my guess would be to do nothing at all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a dynamic array of char
    By Sharke in forum C++ Programming
    Replies: 12
    Last Post: 06-23-2009, 12:25 AM
  2. Vertical Scroller laser cannon problem
    By Swarvy in forum Game Programming
    Replies: 5
    Last Post: 05-02-2009, 06:30 PM
  3. Resource Management question..
    By Raigne in forum C++ Programming
    Replies: 37
    Last Post: 03-08-2008, 08:36 AM
  4. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-03-2005, 11:13 PM
  5. Add and delete functions
    By Ana Val sazi in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2002, 09:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21