Refer Vector::strings

I am in need of some assistance working with STD:Vector of strings



I have a script that has 4 main parts, Read in passed csv file, Store csv in array, iterate through array and preform required math and write output to new csv.



I have achieved 1 and 2, but would like some help with #3:



#3 details:

Each line of csv inputs consists of 11 elements, ex:

"AXB12",45,89,06/06/11,12:00:00 PM,"FLP-02",7.65,309.6,7,101,0.35

"AXB12",45,89,06/06/11,12:05:00 PM,"FLP-02",7.82,310.21,7,101,0.35

"AXB12",45,89,06/06/11,12:10:00 PM,"FLP-02",7.99,310.8,7,101,0.35

"AXB12",45,89,06/06/11,12:15:00 PM,"FLP-02",8.16,311.37,7,101,0.35

"AXB12",45,89,06/06/11,12:20:00 PM,"FLP-02",8.32,311.92,7,101,0.35

"AXB12",45,89,06/06/11,12:25:00 PM,"FLP-02",8.48,312.46,7,101,0.35

"AXB12",45,89,06/06/11,12:30:00 PM,"FLP-02",8.63,312.98,7,101,0.35

"AXB12",45,89,06/06/11,12:35:00 PM,"FLP-02",8.78,313.48,7,101,0.35

"AXB12",45,89,06/06/11,12:40:00 PM,"FLP-02",8.93,313.97,7,101,0.35

"AXB12",45,89,06/06/11,12:45:00 PM,"FLP-02",9.08,314.44,7,101,0.35

"AXB12",45,89,06/06/11,12:50:00 PM,"FLP-02",9.22,314.89,7,101,0.35

"AXB12",45,89,06/06/11,12:55:00 PM,"FLP-02",9.36,315.33,7,101,0.35





I am interested in calling elements 6,7,8. Lets refer to them as DWA,DWB,DWC.



Here is where things get tricky, The number value of DWC determines what function DWA,DWB,DWT get passed to



ex:



if (DWC >0 && DWC < 5) {

SpeedBin1 (DWA,DWB,DWT)

}

elsif(DWC >=5 && DWC < 10) {

SpeedBin2 (DWA,DWB,DWT)

}



I tried to iterate through and setting variables when J was = 6,7 & 8 but no luck.



I am stuck here and would really appreciate any help.



Code is attached:


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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <math.h>
#include <string>
#include <iostream>
#include <sys/stat.h>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;




void readCSV(std::istream &input, std::vector< std::vector<std::string> > &output)
{
        std::string csvLine;
        // read every line from the stream
        while( std::getline(input, csvLine) )
        {
                std::istringstream csvStream(csvLine);
                std::vector<std::string> csvColumn;
                std::string csvElement;

                // read every element from the line that is seperated by commas
                // and put it into the vector or strings
                while( std::getline(csvStream, csvElement, ',') )
                {
                        csvColumn.push_back(csvElement);
                }
                output.push_back(csvColumn);
        }
}



using namespace std;
     int
     main (int argc, char *argv[])
     {
  	string passedValue;
	for(int i = 1; i < argc; i++)
 		passedValue += argv[i];

	std::fstream file(passedValue.c_str(), ios::in);
	
	if(!file.is_open())
        {
                std::cout << "File not found!\n";
                return 1;
        }


        // typedef to save typing for the following object
        typedef std::vector< std::vector<std::string> > csvVector;
        csvVector csvData;

        readCSV(file, csvData);
        // print out read data to prove reading worked
        for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
        {
               


 	for(std::vector<std::string>::iterator j = i->begin(); j != i->end(); ++j)
                {

				
                }
                std::cout << "\n";
        }





  return 0;
}







You can access elements in vectors using []. Remove the for loop on line 65. Write
1
2
if( (*i)[8] > 0 && (*i)[8] < 5 ) ...
else ...

You could also use index instead of iterator in the first loop and write csvData[i][8] to access 8th element in ith row.
Last edited on
I tried to iterate through and setting variables when J was = 6,7 & 8 but no luck.
What do you mean by that?

I'd say on line 67 is something missing like:
1
2
3
if(j != i->begin())
  std::cout << ",";
std::cout << *j;
Topic archived. No new replies allowed.