Q:

Decimal to binary converter c++

I'm creating this program just for fun but i'm havin a problem
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int j = 1;
std::vector<int> n_bin(j);
int n=0; 
int a;
cin>>n;
do
{

    n_bin[j] = n%2;
    n= n/2;
    a++;
    j++;

}while(n>0);

for(j=a; j>0; j--)
{

    cout<<n_bin[j];
}

return 0;

}
the only problem is the output, infact before the real binary number i get a lot of more number for example:
if i assign 104 to n i get: 246978862916874543020108190880108137760108190884026797651687454302000108137760108191524026797651687454302183600828414839627280108137760146978862716874543020001321208800108136960-10108137760108364961101000 where only the last 7 are useful.

A:

n_bin is initialized to have exactly one element, at n_bin[0]. n_bin[j] exhibits undefined behavior for any value of j other than 0, by way of accessing an index out of bounds.

