0

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
}

Edited by Muhammad Faizan_1

2
Contributors
1
Reply
20
Views
2 Years
Discussion Span
Last Post by tinstaafl
1

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 != "");

Edited by tinstaafl

Votes + Comments
Good example of a do...while loop.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.