Why am i getting this error referring to asserts?

Feb 6, 2013 at 10:12pm
So for college we got a programming project where we have to find the values of a vector and part of it is finding the min and max value
so i created a little start out program and it was going well until i got an error when i would try to run it telling me the vector subscript is out of range
what am i doing wrong?
here's my code :

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

int main()
{

vector <double> v;
double x = 0;

while (x != 0)
{
cin >> x;
v.push_back(x);
}

double min = 0;
double max = 1000000000000000;
double median = 2;
double average = 0;

for (int i = 0; i < sizeof(v); ++i)
{
if(v[i] >= max)
max = v[i];
}

cout << "The max value is: " << max << endl;

for (int i = 0; i < sizeof(v); ++i)
{
if(v[i] <= min)
min = v[i];
}

cout << "The min value is " << min << endl;
}
Last edited on Feb 6, 2013 at 10:16pm
Feb 6, 2013 at 10:27pm
I think double is limited to (15 digits). I think your using 16.

http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx
Last edited on Feb 6, 2013 at 10:27pm
Feb 6, 2013 at 10:38pm
I tried reducing it and that didnt work, it must be a different problem
Feb 6, 2013 at 10:41pm
sizeof(v) is NOT the size of the vector. It is the size of the class that holds the vector, not the number of elements in the vector. You want to use v.size()
Topic archived. No new replies allowed.