Assertion Failure Help

Hi Guys,

I am getting an assertion failure for the following code and I have no clue why.

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

#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>

using namespace std;

 int main(){

	double value_just_read_from_file;
	vector <double> P;

	ifstream input_file ("Data.txt");

	if (input_file.is_open ())
	{
			while(input_file >> value_just_read_from_file)
			{
				P.push_back(value_just_read_from_file);
			}
	}
	else
			cout << "Input_file_Data.txt_does_not_exist_in_PWD" << endl;
	cout << "Vector_P_has_" <<P.size() << "_entries, _and_They_are : " <<endl;
	for (size_t i = 0; i <= P.size(); i++)
				cout << P[i] << "\t" ;
	cout << endl;
}


The Data.txt file is just 5 numbers.

Any help would be appreciated.

Thanks for taking a look.
Last edited on
Also I am working on making a program like the one above that will read multiple lines of data but I am pretty lost. I know how many lines their are from the first line of the code but I am unsure how to integrate this into the code as needed.
Last edited on
Please help if you can!!
To say nothing about the rest of your program... This statement will try to read past the end of vector P, causing an assertion error for subscript oorange:

1
2
	for (size_t i = 0; i <= P.size(); i++)
				cout << P[i] << "\t" ;

size() returns the number of elements in the vector. If the size of P is 5, the last valid iterator is 4. Instead:
1
2
	for (size_t i = 0; i < P.size(); i++)
				cout << P[i] << "\t" ;


Yea I figured that out. I added a return(P) at the end. The thing does exactly what it was supposed to now. I am working to enhance it now though and failing miserably. different topic though.
Last edited on
Topic archived. No new replies allowed.