0

I can't figure out why the return value form the "deleteEntry" function is not being output in the last part of my program. It jumps from "Now your list has 5 names again!" to asking if you want to do it again without showing the list without the name you asked it to delete. I know it did it correctly when I tried earlier, but I'm not sure if I changed something without paying attention or if I'm doing something that makes the program do things randomly.

Here's the source code:

#include <iostream>
#include <string>
using namespace std;

int search(string* dynamicArray, int size, string entrytoDelete);

string* addEntry(string *dynamicArray, int&size, string newEntry);

string* deleteEntry(string *dynamicArray, int &size, string entrytoDelete);

int main()
{
  char answer;

  do
    {

      string* name;
      string name2;
      int size = 5;
      int x;
      name = new string[5];

      cout << "\nEnter 5 names:\n";
      for(x = 0; x < size; x++)
    {
      getline(cin, name[x]);
    }
      cout << endl;
      cout << "You entered: \n";
      for(x = 0; x< size; x++)
    {
      cout << *(name + x) << endl;
    }

      cout << "\nEnter another name: \n";
      getline(cin, name2);
      name = addEntry(name, size, name2);
      cout << "\nYour list now has an extra name!\n";
      for(x = 0; x < size; x++)
    cout << x << ": " << name[x] << endl;

      cout<< "\nPick one name to delete: \n";
      getline(cin, name2);
      name = deleteEntry(name, size, name2);
      cout << "\nNow your list has 5 names again!\n";
      for(x = 0; x < size; x++)
    cout << x << ": " << name[x] << endl;

      cout << "\nWould you like to try again? (Y/N)\n";
      cin >> answer;
    }while(answer == 'y' || answer == 'Y');

  cout << "Goodbye.\n";

  return 0;
}


string* addEntry(string *dynamicArray, int&size, string newEntry)
{ 
  string *new_Large = new string[size + 1];

  for(int x = 0; x < size; x++)
    {
      new_Large[x] = *(dynamicArray + x);
    }

  new_Large[size] = newEntry;
  size++;

  delete [] dynamicArray;


  return new_Large;
}

string* deleteEntry(string *dynamicArray, int &size, string entrytoDelete)
{
  int toDelete;
  toDelete = search(dynamicArray, size, entrytoDelete);

  if(toDelete >= 0)
    {
      string *new_Small = new string[size - 1];

      for(int x = 0; x < (size - 1); x++)
    {
      new_Small[x] = *(dynamicArray + x);
      if(dynamicArray[x] != entrytoDelete)
         new_Small[x] = dynamicArray[x];
    }
      size -= size;
      return new_Small;
    }
  else 
    {
      cout << entrytoDelete << " does not exist.\n";
      return dynamicArray;
    }
}

int search(string* dynamicArray, int size, string entrytoDelete)
{
  int toDelete = -1;
  for(int x = 0; x < size; x++)
    {
      if(*(dynamicArray + x) == entrytoDelete)
    {
      toDelete = x;
    }
    }
      return toDelete;
}

I'd really appreciate any help.

10
  • Unrelated to your question you have a memory leak in your program, when you return the new dynamic array to assign to name Commented Apr 30, 2016 at 21:00
  • Why resort to doing this type of coding when you could just use std::vector<std::string>? Commented Apr 30, 2016 at 21:04
  • @Bluasul, you could do all manipulation in one string, separating them by /n, so you could stay away from dynamic allocations on your part. Commented Apr 30, 2016 at 21:04
  • @PaulMcKenzie The program is supposed to make dynamic arrays emulate vectors. Commented Apr 30, 2016 at 21:06
  • 1
    @Bluasul I've been programming for about a month -- I wish that C++ was being taught instead of C. So after a month, the goal is to get you to never use C++ again? No beginner code should be doing this exercise, as it is first not trivial, and second, it gives the person a false sense of satisfaction if they get a "good grade" on something that is subpar or worse, really full of bugs. Commented Apr 30, 2016 at 21:24

2 Answers 2

1

As other mentioned, this code has memory leaks. I can see many flaws in the code. But, to suggest a small change to produce what you want, you may start with the following changes. See the comments in the code:

#include <iostream>
#include <string>
using namespace std;

int search(string* dynamicArray, int size, string entrytoDelete);

string* addEntry(string *dynamicArray, int&size, string newEntry);

string* deleteEntry(string *dynamicArray, int &size, string entrytoDelete);

int main()
{
  char answer;

  do
    {

      string* name;
      string name2;
      int size = 5;
      int x;
      name = new string[5];

      cout << "\nEnter 5 names:\n";
      for(x = 0; x < size; x++)
    {
      getline(cin, name[x]);
    }
      cout << endl;
      cout << "You entered: \n";
      for(x = 0; x< size; x++)
    {
      cout << *(name + x) << endl;
    }

      cout << "\nEnter another name: \n";
      getline(cin, name2);
      name = addEntry(name, size, name2);
      cout << "\nYour list now has an extra name!\n";
      for(x = 0; x < size; x++)
    cout << x << ": " << name[x] << endl;

      cout<< "\nPick one name to delete: \n";
      getline(cin, name2);
      name = deleteEntry(name, size, name2);
      cout << "\nNow your list has 5 names again!\n";
      for(x = 0; x < size; x++)
    cout << x << ": " << name[x] << endl;

      cout << "\nWould you like to try again? (Y/N)\n";
      cin >> answer;
    }while(answer == 'y' || answer == 'Y');

  cout << "Goodbye.\n";

  return 0;
}


string* addEntry(string *dynamicArray, int&size, string newEntry)
{ 
  string *new_Large = new string[size + 1];

  for(int x = 0; x < size; x++)
    {
      new_Large[x] = *(dynamicArray + x);
    }

  new_Large[size] = newEntry;
  size++;

  delete [] dynamicArray;


  return new_Large;
}

string* deleteEntry(string *dynamicArray, int &size, string entrytoDelete)
{
  int toDelete;
  toDelete = search(dynamicArray, size, entrytoDelete);

  if(toDelete >= 0)
    {
      string *new_Small = new string[size - 1];

      for(int x = 0, y = 0; x < size; x++)   //<- start of changes
    {                                        //
      // new_Small[x] = *(dynamicArray + x); //<- remove this
      if(dynamicArray[x] != entrytoDelete)   //
         new_Small[y++] = dynamicArray[x];   //<- x to y++
    }                                        //
      --size;                                //<- end of changes
      return new_Small;
    }
  else 
    {
      cout << entrytoDelete << " does not exist.\n";
      return dynamicArray;
    }
}

int search(string* dynamicArray, int size, string entrytoDelete)
{
  int toDelete = -1;
  for(int x = 0; x < size; x++)
    {
      if(*(dynamicArray + x) == entrytoDelete)
    {
      toDelete = x;
    }
    }
      return toDelete;
}
Sign up to request clarification or add additional context in comments.

Comments

1

In your deleteEntry:

size -= size;

This will always set size = 0. I think you're looking to do:

size--;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.