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();
}
| |