Bluasul
Bluasul

Reputation: 325

C++: Program not outputting function return value

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.

Upvotes: 0

Views: 109

Answers (2)

Eissa N.
Eissa N.

Reputation: 1725

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

Upvotes: 1

georgeofallages
georgeofallages

Reputation: 504

In your deleteEntry:

size -= size;

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

size--;

Upvotes: 1

Related Questions