C ++:程序不输出函数返回值

时间:2016-04-30 20:50:02

标签: c++

我无法弄清楚为什么返回值形成" deleteEntry"我的程序的最后一部分没有输出函数。它跳出#34;现在你的列表又有5个名字!"询问您是否要再次执行此操作而不显示没有您要求删除的名称的列表。我知道它在我早些时候尝试时做得很好,但是我不确定我是否在没有注意的情况下改变了一些东西,或者我是否正在做一些让程序随机做事的事情。

这是源代码:

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

我真的很感激任何帮助。

2 个答案:

答案 0 :(得分:1)

在你的deleteEntry中:

size -= size;

这将始终设置size = 0.我认为你想做:

size--;

答案 1 :(得分:1)

正如其他提到的,这段代码有内存泄漏。我可以在代码中看到许多缺陷。但是,为了建议一个小的变化来产生你想要的东西,你可以从以下变化开始。请参阅代码中的注释:

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