Question: I'm writing C++ code for a graphics program and in it I have to collect all coordinates to later parse them.I'm trying to use vectors for the same but the following error is returned:

> 
> Debug Assertion failed  
> 
>  Program:....  
> 
>  File:....  
> 
>  Line 932  
> 
>  Expression:Vector subscript out of range
> 
> 
> 

Here's part of my code:

```
std::vector<float> coordinates;
//random code
if(dnew >= 0)
{
   x = x + 1.0;

   glPointSize(1.5f);
   glColor3f(0.0,1.0,1.0);
   glBegin(GL_POINTS);
   glVertex2f(x, y);
   glEnd();
   dnew = dnew + a;
   if(t == 1)
   {
       coordinates.push_back (x);
   }
}

cout <<coordinates[1];

```

When I press retry it takes me to stdthrow.cpp and points to the following line:

```
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const     wchar_t *file, unsigned int line)
{
    // report error and die
    if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
    {
        ::_CrtDbgBreak();
    }
}

```

Answer: 
This error can be caused if `coordinates.size()<= i`, the exception is raised which gives you information that you tried to access element out of bounds of vector
You only push one element into coordinates, so the only valid index is `0`. `coordinates[1]` is thus one past the end of the vector.

Further there are two conditions that could prevent the one `push_back` that is there at all from happening, so it might very well be, that your vector is empty. But you access the second element of the vector unconditionally.