I am trying to write a program that takes unlimited input of names using dynamic memory and stop when the first character is the enter key. i tried, but i am unable to do it.

#include "ReadString"
void main()
{

const int NumNames(20);
char **pNames;
pNames=new char [NumNames++];


for (i=more;i<NumNames;i++)
{
do{
i++;
cout<<i<<")";
pNames[i]=ReadString();
}while(cin.get()='\n';

more=NumNames;
NumNames+=NumNames
}

The easiest way is with a vector<string>. You can keep adding names until adding a new name causes a memory overflow error.

Checking for \n won't work cin automatically delimits the retuirn with \n and ignores it. A better option would be to use getline. This will return "" if there is only \n entered:

vector<string> names;
string newname = "";
do
{
    cout << "Add a new name:";
    getline(cin, newname);
    if (newname != "")
    {
        names.push_back(newname);
    }
} while (newname != "");
commented: Good example of a do...while loop. +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.