Here's my code:
int amount;
cout<<"How many books are you donating? ";
cin>>amount;
string donation[amount];
cout<<"Enter the titles:"<<endl;
for (int i=0; i<amount; i+=1)
{
getline(cin.ignore(numeric_limits<streamsize>::max(), '\n'), donation[i]);
}
cout<<endl<<"You donated these:";
for (int j=0; j<amount; j+=1)
{
cout<<donation[j]<<endl;
}
This accepts 5 values even though I only input 3 for amount
. Then it prints only values 1, 3, and 5.
If I just write it as getline(cin.ignore(), donation[i]);
or getline(cin.ignore(1, '\n'), donation[i]);
, then the output becomes like this:
How many books are you donating? 3
Enter the titles:
book1
book2
book3
You donated these:
book1
ook2
ook3
What should I write in the cin.ignore()
for it to ignore just the \n
value?