I'm attempting to read in a file with the following format: 1,2,4,6,8,12,12, . I want to use getline() and a stringstream object to delimit the input and store it into a vector after converting to integer. It works and I can see that by the output being capable of adding with the number one but it still throws the exception after it's done converting all of the numbers in the file.
Output: 2 3 5 7 9 13 13
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;
int main (int argc, char** argv){
ifstream infile;
vector <int> vect;
infile.open("tested");
if(!infile.is_open()){
cout<<"File did not open"<<endl;
}
else{
while(!infile.eof()) {
string line;
getline(infile, line);
stringstream ss(line);
while (ss){
string p;
getline(ss, p, ',');
int x = stoi(p);
cout<<x+1<<endl;
vect.push_back(x);
}
}
int i=0;
while(i<vect.size()){
int e = vect[i];
cout<<e<<endl;
i++;
}
sort(vect.begin(), vect.end());
int j=0;
while(j<vect.size()){
int n = vect[j];
cout<<n<<endl;
j++;
}
cout<<"end reached"<<endl;
}
}