Text file and vectors

Hi

I have a text file that has 9 columns. How do I access columns 7 and 8 and multiply these values together? My text file only consists of numbers. Both int and double types.

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
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

using namespace std;

int main()
{
ifstream infile("work.txt");
if (!infile)
    {
        cout << "Unable to open file" << endl;
    return -1;
    }

vector <vector<double> > myVector;

        string line;

        while (getline(infile, line))
            {
            myVector.push_back(vector<double>());

            stringstream input(line);
            double value;

            while (input >> value)
                myVector.back().push_back(value);
            }
}
Last edited on
Isn't this a duplicate of your latest post to http://www.cplusplus.com/forum/beginner/127931/ ?

Please don't spam the forums with multiple threads on the same topic.
This involves vectors
After line 30, myVector[6] and myVector[7] will contain columns 7 and 8 respectively. You can do the following:
1
2
3
 
  double rslt; 
  rslt = myvector[6] * myVector[7];

However, this is true only after reading the first row. When you read the second row, you continue pushing values onto the vector. Therefore, columns 7 and 8 will be in myvector[15] and myVector[16].

You can accomodate this by keeping your own index and incrementing it by 9 for each row your read, or your can clear the vector at the bottom of the read loop which would assume that you don't have further need for the contents of the row after having processed it.


Edit: I missed that myVector was a vector of vectors of doubles.
Last edited on
1
2
3
double result;
    result = myVector[6] * myVector[7];
    cout << result << endl;


When I try this I get an error message:

error: no match for 'operator*' in 'myVector.std::vector<_Tp, _Alloc>::operator[]<std::vector<double>, std::allocator<std::vector<double> > >(6u) * myVector.std::vector<_Tp, _Alloc>::operator[]<std::vector<double>, std::allocator<std::vector<double> > >(7u)'|

Not sure what wrong?
myVector[6] will return you a vector of doubles (as far as i understand it).
similarly myVector[7] will do the same.

You're then asking your compiler to effectively multiply together two collections of doubles. you need to tell the compiler how this happens i.e. overload the multiplication operator.
Last edited on
How does one fix this?
I told you. overload the multiplication operator. Or get the doubles out directly.

edit: you could do something like this to get your doubles out:

double myNumber = (myVector[6])[X];

This will get you your X'th double in the 6th vector from your vector of vectors.

It looks horrible though...
Last edited on
Here's an example of your issue i've quickly knocked up:

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
#include <vector>

int main() 
{ 
	// our vector of vectors of doubles.
	std::vector <std::vector<double>> myVector;

	// create a vector of doubles and populate it with some data
	std::vector<double> vec1;
	for(int i=0;i<10;++i)
	{		
		vec1.push_back(static_cast<double>(i));
	}
	
	
	// create another vector of doubles and populate it with some data
	std::vector<double> vec2;
	for(int j=10;j<20;++j)
	{		
		vec2.push_back(static_cast<double>(j));
	}

	// we now have 2 vectors of doubles. Add these both to our 
	// vector of vectors of doubles.
	myVector.push_back(vec1);
	myVector.push_back(vec2);

	// I've commented out as this makes no sense to the compiler
	// as 'myVector[0]' will return me a vector, NOT a double.
	//double silly = myVector[0]; 

	// get me the 4th double from the first vector we put into our vector of vectors.
	double result = (myVector[0])[4];  

	return 0;
}
Thanks I understand this. I made a for loop with variable i and said [i][6] and [i][7] and got the correct answers.
Thanks
You're welcome :)
From the calculated values (result) how would you determine the largest value in that set?
can you post your most up to date code please?
I got it to work however i had to place the same statement inside and outside the while loop. Can someone please explain this?
1
2
    if (product>temp)
        temp = product;


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
vector <vector<double> > myVector;

        string line;

        while (getline(infile, line))
            {
            myVector.push_back(vector<double>());

            stringstream input(line);
            double value;

            while (input >> value)
                myVector.back().push_back(value);
            }

    double product = 0;
    double energy =0;
    double temp=0;
    double sum_product;
    for (int i=0; i<myVector.size(); i++)
    {
    product = myVector[i][6] * myVector[i][7];
    sum_product=sum_product+product;
 //   cout << product << endl;

    if (product>temp)
        temp = product;    
    }
//    cout << endl << sum_product << endl;

    double avg=0;
    avg=sum_product/myVector.size();
    cout << avg << endl;

    {
    if (product>temp)
        temp = product;
    cout << endl << temp;
    }
}
Last edited on
Lines 36-37 should be unnecessary. product is only changed at line 22 (within the loop), therefore your test for highest should also be inside the loop. What happens when you eliminate lines 36-37?
Topic archived. No new replies allowed.