-4

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.

code_dredd
  • 5,368
  • 1
  • 21
  • 48
Jeff Ekaka
  • 65
  • 5
  • 2
    Possible duplicate of [Why does std::getline() skip input after a formatted extraction?](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Jean-Baptiste Yunès Sep 04 '16 at 21:23
  • 1
    `string library[nbBooks]` is not C++ (which does not have VLA, as C does): http://stackoverflow.com/q/1887097/1116364 – Daniel Jour Sep 04 '16 at 21:24
  • 2
    @KenWhite if `nbooks==5` then the first index used is `4`, loop contains a decrementation. – Jean-Baptiste Yunès Sep 04 '16 at 21:26
  • 1
    I'm fairly new to C++, but I thought nbBooks would need to be a constant. – ethan codes Sep 04 '16 at 21:27
  • 1
    @KenWhite and? expression for `while` is evaluated so the value of the expression is 5, and in the loop the value of `nBooks` is 4. Next loop, value of the expression is 4, but inside the loop value is 3... – Jean-Baptiste Yunès Sep 04 '16 at 21:29
  • @Jean-BaptisteYunès Re the duplicate .. I fail to see how this leads to `library[0]` being empty though: It's read last. Shouldn't the first read entry (`library[nBooks - 1]`) contain an empty string (due to the left over newline)? – Daniel Jour Sep 04 '16 at 21:33
  • 1
    @DanielJour You are right...but it seems that the code is not what OP really tested as vlas not supported... – Jean-Baptiste Yunès Sep 04 '16 at 21:38

3 Answers3

1

arrays must has a Specific size at compile time not at runtime. this code will issue an error flag: "error C2133: 'library' : unknown size" if you want to allocate an array with a size assigned at runtime then:

use the memory HEAP:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int nbBooks;

    cout << "nBooks: ";
    cin >> nbBooks; // nBooks is not defined until runtime
    cout << endl;

    cin.sync(); // flushing the input buffer

    string* library = new string [nbBooks]; // allocating pointer to array on the heap memory not the stack
    int i = 0;

    while(nbBooks--) 
    {
        cout << "library[" << i << "]: ";
        getline(cin, library[i]);
        i++;
        cin.sync(); // flushing again the buffer remeber "safe programming is the purpose of any programmer"
    }

    cout << "library[0]: " << library[0] << endl;

    // now remember memory of heap is not unallocated by the compiler so it must be fred by the programmer

    delete[]library; //dont forget "[]"

    return 0;
}

now compile the code and everything will work correctly.

I use cin.sync() right after cin>> nbooks; to ensure FLUSHING the input buffer. and once again inside the loop after any assignment to the elements I used another to ensure flushing the buffer. as I know there's an error in getline which doesn't flush the input buffer totally so some data will affect other variable, to overcome this problem we use cin.sync() to ensure emptying the buffer.

Raindrop7
  • 3,805
  • 3
  • 14
  • 27
0

Alright, I am a bit confused : a simple cin.ignore() before the getline() function made it work now, while the same thing wasn't working 10 minutes ago (eventhough I was rebuilding and recompiling each time)(and I swear to god I am not idiot enough to forget that ;) )... So sorry for my pretty useless question... Oh by the way :

while(nbBooks--) {...}

It evaluates the value nbBooks, then decrement it, so no problem here.

Jeff Ekaka
  • 65
  • 5
0

Well, the problem is with the \n character which is staying in the buffer after this cin >> nbBooks; line gets executed. The operatot>> method of cin does not accept \n by default thus the newline character is not pulled out from the buffer. So, you have to pull it out, right?. Just add cin.get(); after cin >> nbBooks;. The get() method will extract the \n character. Rest of the code is fine.

Parnab Sanyal
  • 644
  • 5
  • 17