Segmentation Fault: Dynamic Array of Strings

This program is supposed to allow the user to enter 5 names and then add one, delete one, and try to delete one that's not on the list. I can add one, but keep getting "segmentation fault" after trying to delete one. Any suggestions?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include<iostream>
#include<string>
using namespace std;

void read5names(string* names, int size);
void displayNames(string* names, int size);
string* addEntry(string* dynamicArray, int &size, string newEntry);
string* deleteEntry(string* dynamicArray, int &size, string entryToDelete);
int findName(string* dynamicArray, int size, string entryToDelete);

int main()
{
  //declarations
  string* names;
  string newname;
  int size = 5;
  int i;
  names = new string[5];

  //read 5 names
  cout << "Enter 5 names, one per line\n";
  read5names(names, size);
  cout << "\n===== ORIGINAL LIST =====\n";
  displayNames(names, size);

  //add a name and resize
  cout << "\nEnter a new name to add\n";
  cin >> newname;
  names = addEntry(names, size, newname);
  cout << "\n=====  LONGER LIST  =====\n";
  displayNames(names, size);

  //remove a name and resize - attempt 1
  cout << "\nEnter a name to delete\n";
  cin >> newname;
  names = deleteEntry(names, size, newname);
  cout << "\n===== SHORTER  LIST =====\n";
  displayNames(names, size);

  //remove a name and resize - attempt 2
  cout << "\nEnter a name to delete that is NOT in the list\n";
  cin >> newname;
  names = deleteEntry(names, size, newname);
  cout << "\n=====  FINAL  LIST  =====\n";
  displayNames(names, size);

  return 0;
}

void displayNames(string* names, int size)
{
  int i;
  for (i = 0; i < size; i++)
    cout << names[i] << endl;
}

void read5names(string* names, int size)
{
  int i;
  for (i = 0; i < size; i++)
    cin >> names[i];
}

string* addEntry(string* dynamicArray, int &size, string newEntry)
{
  //declare new array one element larger
  string *newArr;
  newArr = new string[size + 1];

  //copy names over into new array
  int i;
  for (i = 0; i < size; i++)
    newArr[i] = dynamicArray[i];

  //add the new name
  newArr[size] = newEntry;

  //increase the size
  size++;

  //delete the old array
  delete[] dynamicArray;

  //return the pointer to the new array
  return newArr;
}

string* deleteEntry(string* dynamicArray, int &size, string entryToDelete)
{
  //determine if entryToDelete is in the dynamicArray
  int location;  //location of the match
  location = findName(dynamicArray, size, entryToDelete);
  if (location >= 0) //name found, now delete it
  {
    //declare new array one size smaller
    string *newArr;
    newArr = new string[size - 1];

    //copy names over into new array
    int i, index;
    for (i = 0; i < size; i++)
    {
      if (i != location)
      {
        newArr[index] = dynamicArray[i];
        index++;
      }
    }

    //decrement size
    size--;

    //delete the old array
    delete[] dynamicArray;

    //return the pointer to the new array
    return newArr;
  }
  else //name not found
  {
    cout << entryToDelete << " not found!\n";

    //return the original array pointer
    return dynamicArray;

  }
}

int findName(string* dynamicArray, int size, string entryToDelete)
{
  int i;
  for (i = 0; i < size; i++)
  {
    if (dynamicArray[i] == entryToDelete)
    {
      return i;
      break;
    }
  }
  return -1;
}
Last edited on
just found the error:
I never initialized index to 0
Topic archived. No new replies allowed.