Input of a list of ints

Hello,
I want to read multiple lists of ints, so I made a function that could do it for me:

(I have iostream, sstream, vector and string included)
1
2
3
4
5
6
7
8
9
void Read(vector<int> *V)
{
    string S;
    int i;
    cin.ignore();
    getline(cin, S);
    istringstream T(S);
    while (T >> i) {V->push_back(i);}
}


Now the first call works nicely. But the second one doesn't read the first value and the last number stored in the vector is a value that we're probably not supposed to access as it changed pretty much every time I ran the program.

By the way I write the values like this (for example):
1 2 3 4

(So a spacebar press between every value and enter at the end)

If it can help I use G++ and have it follow the C++11 ISO.

Thank you for your help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <sstream>
#include <string>

int main()
{
    std::cout << "Enter numbers: \n";
    std::string S;
    getline(std::cin, S);
    std::istringstream T(S);
    int i{};
    std::vector<int>* V = new std::vector<int>;

    while(T >> i)
    {
        V ->push_back(i);
    }
    for (auto& elem : *V)
    {
        std::cout << elem << " ";
    }
    delete V;
}
Sorry but I don't really undertstand what this has to do with my problem.
And with more testing I found out that the problem is exactly the same with every call after the first one.
Last edited on
Now the first call works nicely.

The problem is, you have something else in your program, probably in main() which leaves some debris behind in the input buffer. Rather than clearing up that problem at the point where it happened, instead you put the 'fix' inside this function, at line 5
 
    cin.ignore();


You should remove that line and instead put it where it rightly belongs, just after whichever use of cin >> has left the buffer with a trailing newline character.
Thank you, it works now. Could you just explain to me why I can't simply clean the input buffer whenever I want ? (Because the problem was that I cleaned it too many times)
Isn't it supposed to only empty it ? (Or is there something I don't understand about the input buffer)
Typically, there are two types of input using cin.
1.
1
2
    string word;
    cin >> word;

and 2.
1
2
    string line;
    getline(cin, line);


In each case the program reads a string. But the state of the input buffer is different afterwards. The first version stops reading as soon as it encounters any whitespace. It will leave a trailing newline character '\n' in the input buffer.

The second version will keep on reading until it finds the delimiter (by default a newline '\n') which it extracts from the buffer and discards.

So, the use of cin.ignore(). It is only required before getline() if the previous input operation left the newline there. But the function Read() has no way of knowing what the previous input was, so it should not attempt to deal with it. You need to use ignore after the cin>> which was the cause of the problem. Not before the getline() which did not cause the problem.
Understood, thank you.
Topic archived. No new replies allowed.