0

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;      
 }
uchman21
  • 638
  • 2
  • 6
  • 21
  • See http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – chris Apr 17 '15 at 16:50
  • I've seen the answer but, i'm not using any cin before the getline – uchman21 Apr 17 '15 at 18:09
  • It's not `cin`, it's the formatted extraction (`operator>>`). – chris Apr 17 '15 at 18:10
  • i changed the if( iss >> value ) to if( (iss >> value ).get()) and even if(( iss >> value ).ignore()) but instead it got worse .get() skipped 2 columns while .ignore() returned only the first number. – uchman21 Apr 17 '15 at 18:20

1 Answers1

0

Finally i got to to work by changing the code. By replacing

   while(getline(in, line))
 {
   line_contents.clear(); 
   istringstream iss(line);
  do
  {
    int value;

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

 }

with this:

while(getline(in, line))
{
  line_contents.clear();
istringstream iss(line);
    int value,value2,value3;
    char valuea,valueb;

    do{
    if( iss >> value >> valuea >>value2>> valueb>>value3)
    {
        line_contents.push_back(value);
        line_contents.push_back(value2);
        line_contents.push_back(value3);
    }}while(!iss.eof());
array.push_back(line_contents); 

 }

Read all the values manually through the formatted extraction

uchman21
  • 638
  • 2
  • 6
  • 21
  • you should accept this answer so others can see that your problem was solved ( yes, you can accept your own answers: http://stackoverflow.com/help/self-answer ) – Erik Apr 17 '15 at 20:31
  • I can only accept it in two days according to the site – uchman21 Apr 17 '15 at 21:50