I have been looking at this code for 2 days now but still can't find where i went wrong. Searched online and applied all methods but it still skips the first column. For example if i have
first line ->1,2,3,4 second line->5,6,7,8 and third line->9,10,11,12.
The output it gives is
first line ->1,2,3,4 second line->6,7,8 and third line->9,10,11,12.
And don't know why it also stores them in one vector and not in a 2d vector but that is not really an issue as i can work around it.
Thanks a lot for help.
#include <iostream>
#include <fstream>
#include <sstream>
#include<string>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int> > array;
vector<vector<int> > readCSV(string filename)
{
vector<vector<int> > contents;
vector<int> line_contents;
ifstream in(filename);
if(in.is_open()){
string line;
while(getline(in, line))
{
line_contents.clear();
// contents.push_back(vector<int>());
//vector<int> line_contents;// = contents[contents.size()-1];
istringstream iss(line);
do
{
int value;
//line_contents.clear();
// iss.ignore();
//string fieldend;
// getline(iss, fieldend, ',');
if( iss >> value )
{
line_contents.push_back(value);
}
else
{
iss.clear();
}
// iss.clear();
string fieldend;
getline(iss, fieldend, ',');
// read to separator
} while(!iss.eof());
array.push_back(line_contents);
//contents.push_back();
//contents.push_back( vector<int>());
}
in.close();
}
else{
cout<<"Couldn't open input file... make sure the file is in the cwd"<<endl;
}
return contents;
}
int main()
{
// int size;
int max=0;
ofstream F1 ("inputDBX.csv");
ofstream F2 ("inputDBX2.csv");
readCSV("inputDB.csv");
if(F1.is_open() && F2.is_open()){
// print the array
for(int i=0; i<array.size();)
{
F1<<array[0][i]<<","<<array[0][i+1]<<","<<array[0][i+2]<<","<<array[0] [i+1]<<","<<array[0][i+2]<<endl;
if (max< array[0][i+2]){
max= array[0][i+2];
}
else{
F2<<array[0][i]<<","<<"0.0"<<","<<array[0][i+2]<<","<<array[0][i+1]<<","<<max+0.5<<endl;
}
i=i+3;
}
F1.close();
F2.close();
}
else{
cout<<"Unable to open/create one/both output files;"<<endl;
}
// cout<<array.size()<<"x"<<array[1].size()<<endl;
for(int i=0; i<array.size(); ++i)
{
for(int j=0; j<array[i].size(); ++j)
{
cout << array[i][j] <<" ";
}
cout << endl;
}
return 0;
}