0

I am trying to have my code take the number of authors who worked on a book and then have an array be filled based on however many authors there are. However, whenever I have my getline statement so it can get their whole name, the input is skipped over.

cout << "How many authors are there: ";
cin >> uNumOfAuthor;
cout << endl;

string* uAuthor = new string[uNumOfAuthor];

if (uNumOfAuthor == 0)
        cout << "Invalid number of authors.\n";

else if (uNumOfAuthor == 1)
{
    cout << "Enter the author's name: ";
    cin >> uAuthor[0];
}

else
{
    for (int i = 0; i < uNumOfAuthor; i++)
    {
        cout << "Author " << i << "'s name: ";
        getline(cin, uAuthor[i]);
    }
}

It works if I only do the first name and just use cin >> uAuthor.

Ludicro
  • 3
  • 1
  • 1
    Intermixing `cin >> x;` and `getline(cin, s);` means that the stream may not quite be in the desired state for the subsequent operation. – Eljay Oct 15 '20 at 17:47

0 Answers0