Strange error Iterating through vector

Hi, this is just a part of a program that I'm working on. I stopped because I've come across an error I've never seen before.
The first input from the console is the number of subsequent lines the user plans to enter. Each line is stored into a vector of ints (lineVector). I want to store all the elements in this vector except for the first one. I do this by initializing my new vector (tickets) with (lineVector.begin()+1, lineVector.end()).
For some reason, evertime I proceed in the loop and read a new line, my vector tickets starts to ignore another element at the front of lineVector.
For example I input 3 to input three lines. Then I enter 1, 2, 3, 4, 5. In the first iteration, tickets is initialized to 2, 3, 4, 5 as desired. If I enter 1, 2, 3, 4, 5 again in the second iteration, tickets is initialized to 3, 4, 5. Again, if i enter 1, 2, 3, 4, 5 in the third iteration, tickets is initialized to 4, 5.
I have a feeling the problem comes from the +1 in the initialization of tickets because I have never used that before.
Thanks for the 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
25
26
27
28
29
30
31
32
33
34
35
36
  #include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main()
{

	int lineNumber; 
	cin >> lineNumber;
	

	for(;lineNumber >0; lineNumber--)
	{
		cin.ignore();//to get rid of hanging new line
		vector<int> lineVect;
		string line;
		getline(cin, line);

		istringstream lineStream(line);
		int intHolder;

		while(lineStream >> intHolder)
			lineVect.push_back(intHolder);

		vector<int> tickets (lineVect.begin()+1, lineVect.end());

		for(int i: tickets)
			cout << i << endl;
		
	}	
	
	cin.ignore();

}
Could you explain your goal a little more clearly? This code looks as if it could benefit from an alternate approach.

EDIT: I think I get what you wanted to do...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <limits>

using namespace std;
int main()
{
    int lineNumber;
    while ((cout << "How many lines?: ") &&
           (!(cin >> lineNumber) || lineNumber < 0))
    {
        cout << "Try a positive number!" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    for(int i = 0; i < lineNumber; i++)
    {
        vector<int> lineVect;
        string line;
        cout << "Enter a line of numbers: ";
        getline(cin, line);

        istringstream lineStream(line);
        int intHolder;

        while(lineStream >> intHolder)
            lineVect.push_back(intHolder);

        vector<int> tickets (lineVect.begin() + 1, lineVect.end());

        for(int j: tickets)
            cout << j << endl;

    }

    return 0;
}


For what it's worth, in the code you posted originally, move the cin.ignore() you have on line 16 from inside the for loop to right above the for loop. The output becomes closer to what you're expecting. On the first iteration of the loop, the cin.ignore() is casting out the newline left in the buffer from reading in how many lines you want. However, on every subsequent iteration, it is throwing out the first number on your line. Then, by doing the +1 stuff in the tickets vector constructor you're tossing the second item on the line.
Last edited on
Thanks for your reply.
But could you exactly what
cin.ignore(numeric_limits<streamsize>::max(), '\n');
does and how adding it fixes the problem?

In this case, how does it function differently from just cin.ignore();
?
Check my edit. :) I don't think adding all that streamsize stuff made the difference. It is the placement of the cin.ignore() that is most important for solving your problem.
OOHHHHH wow that's very cool.
Thanks a lot!
And I'm not sure why I missed your edit.
Last edited on
Topic archived. No new replies allowed.