I am a bit ashamed with this, but really I can't see what's not working properly with this code. For now it should only store some book names (hence the array and the getline()
), and the first cin
indicates how many of them I am going to store. But I don't know why, if I enter a number N
for nbBooks
, I am only able to enter N-1
book names, and library[0]
(last book entered) is just a space.
#include <iostream>
using namespace std;
int main()
{
int nbBooks;
cin >> nbBooks;
string library[nbBooks];
while(nbBooks--) {
getline(cin, library[nbBooks]);
}
cout << library[0];
return 0;
}
I know there must be something with getline()
, but even though I did search answers about this I couldn't find any.