Getting Separate Vectors from txt File

Pages: 12
I am putting in an input with 4 lines of data. I get an assertion error after the second time function(T,P[i]) is run.
Hrmmm... I am not. But memory leaks can be that way.

Were you testing with the code I just posted, or is this some other code?

If it's other code, please post your code.

If it is the same code, please post your "Data.txt" file.
Wow you have gotten me so close I am starting to get giddy. Everything is working except for that assertion error. As I said before I can't get through function(time,P[i]) the second time around or when i =1.
Last edited on
Can you post your Data.txt file?
Last edited on
Here is the code I am using. I checked it against the one you posted and I couldn't see any differences.

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
vector < vector<double> > get_data(){

	double value_just_read_from_line;
	vector< vector<double> > P(0);
	int lineCount = -1;
	string line;
	int lengthOfLineTerminator = 2;

	ifstream input_file ("Data.txt");

	if (input_file.is_open ())
	{
		input_file >> lineCount;
		input_file.ignore(lengthOfLineTerminator);

		int index = 0;
		while (!input_file.eof()) {
			getline(input_file, line);	// read in the line from the file
			stringstream stream(line);
			vector<double> v;
			while (stream >> value_just_read_from_line) {
				v.push_back(value_just_read_from_line);
				if (input_file.peek() == '\r' || input_file.peek() == '\n')
					break;
			}
			P.push_back(v);
			index++;
		}
	}
	else
			cout << "Input_file_Data.txt_does_not_exist_in_PWD" << endl;

	cout << "There are" <<P.size() << " Cash Flows, _and_They_are : " <<endl;
	for (size_t i = 0; i < P.size(); i++) {
		cout << "Cash Flow #" << i+1 << ": " << endl;
		vector<double> vec = P[i];
		for (size_t j = 0; j < vec.size(); j++)
				cout << P[i][j] << " ";
		cout << endl;
	}
	cout << endl;
	return(P);
}

vector<double> buildTimeVector(vector<double> incomingVector, vector<double>& outgoingVector) {
	for(double d = 0; d < incomingVector.size(); d++) {
		outgoingVector.push_back(d);
	}
	return outgoingVector;
}

double function(vector<double>& time, vector<double>& value_of_data_at_that_time) {
	for(int i = 0; i < time.size(); i++)
		cout << "For position " << time[i] << " the vaule is " << value_of_data_at_that_time[i] << endl;
	return 0;
}

int main(){
	
	vector < vector<double> > P;

	vector <double>  CF;
	vector <double>  T;
	P = get_data();
	for(int a=0; a<P.size();a++){
	buildTimeVector(P[a],T);
	function(T,P[a]);

	cout << endl;
	}
	cout<<endl;
	return 0;
}
Last edited on
My data file is just something I have saved in a notepad and it is:


3

1090 5 -1000 60 50 56 1057.77
1000 5 90 700 3 3 3 3 3 3
2 22 2 2 2 2 2
Aha! Just as I thought. :)

Take a look at line 60. T is being reused each time. So the numbers keep getting tacked on to the end of the vector. So if you have 1 item on line "0" and 3 items on line "1" your vector will look like "0 0 1 2" instead of "0 1 2".

So you have two options. Declare T within the "for loop", or clear out the vector before using it again (i.e. add outgoingVector.clear(); on your line 46).

Give either way a shot, and let me know how you fare.
Topic archived. No new replies allowed.
Pages: 12