#include <iostream>
#include <string>
#include <new>
#include <cstdlib>
usingnamespace std;
int main(){
int i = 1;
do{
string * p;
p = new (nothrow) string[i];
cout << '\t' << '\t' << "Choose an option below";
cout << endl << "Enter new name - A";
cout << endl << "Browse names - B";
cout << endl << "Delete a name - C";
cout << endl << endl << "Enter choice -> ";
string choice;
getline(cin, choice);
if (choice == "a" || choice == "A"){
int n=i;
if (p == 0)
cout << "Error: memory could not be allocated.";
else {
for(n=i;n==i;i++){
cout << "Enter name -> ";
getline(cin, p[i]);
i++;
}
cout << p[n];
cin.get();
system("clear");
for (n=0; n<i; n++)
cout << p[n] << ", ";
cin.get();
}
}
}while(1==1);
cin.get();
}
EDIT:
Your problem is here:
getline(cin, p[i]);
p[i] is out of bounds of your array. Your array can only go from p[0] to p[i-1]. p[i] is passed the end of the array, therefore you're trying to modify a string that hasn't been constructed, and are corrupting the heap and doing all sorts of other nasty stuff that is causing the segfault.
You seem to be doing a lot of questionable things here. Why are you creating an array of strings if i is always 1? Why not just use a single string?
What is the point of this loop: for(n=i;n==i;i++){ ?
If I were you, I'd get rid of that loop, get rid of the new[] line, and just use a normal string:
I is not always 1. It is initialized as 1 before the loop begins. And, at the end of the loop, i++. So when you are entering another name, i=2. so on and so forth.